Skip to content

Commit 6b2a410

Browse files
Merge branch 'topic/fix-sem-diags' into 'master'
Avoid using dangling pointer in semantic diags job See merge request eng/ide/ada_language_server!2300
2 parents 3c68ac8 + 94fc1f1 commit 6b2a410

6 files changed

Lines changed: 135 additions & 6 deletions

File tree

source/ada/lsp-ada_semantic_diagnostics.adb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ with Libadalang.Semantic_Diagnostics;
2525
with VSS.Strings.Conversions;
2626

2727
with GNATCOLL.Traces; use GNATCOLL.Traces;
28-
with GNATCOLL.VFS;
2928

3029
with LSP.Ada_Context_Sets;
3130
with LSP.Ada_Documents.Semantic_Diagnostics;
@@ -164,7 +163,7 @@ package body LSP.Ada_Semantic_Diagnostics is
164163
-- closed while this job was in the queue: discard it.
165164
Me_Debug.Trace
166165
("Cancelling semantic diagnostics job for "
167-
& Self.Handler.To_File (Self.Document.URI).Display_Base_Name
166+
& Self.File_Name.Display_Base_Name
168167
& " because the project was reloaded, server is shutting down, or document was closed");
169168
Free (Self.Cursor);
170169
Status := LSP.Server_Jobs.Done;
@@ -181,7 +180,7 @@ package body LSP.Ada_Semantic_Diagnostics is
181180

182181
Me_Debug.Trace
183182
("Cancelling semantic diagnostics job for "
184-
& Self.Handler.To_File (Self.Document.URI).Display_Base_Name
183+
& Self.File_Name.Display_Base_Name
185184
& " because the document was edited since it was enqueued");
186185
Free (Self.Cursor);
187186
Status := LSP.Server_Jobs.Done;
@@ -205,7 +204,7 @@ package body LSP.Ada_Semantic_Diagnostics is
205204
if not Self.Ranges.Is_Empty then
206205

207206
Me_Debug.Trace ("Executing a per-range semantic diagnostics job for "
208-
& Self.Handler.To_File (Self.Document.URI).Display_Base_Name
207+
& Self.File_Name.Display_Base_Name
209208
& " (version "
210209
& Self.Document_Version.Value'Image
211210
& ")");
@@ -273,8 +272,7 @@ package body LSP.Ada_Semantic_Diagnostics is
273272
begin
274273
Me_Debug.Trace
275274
("Semantic diagnostics traversal completed for "
276-
& Self.Handler.To_File (Self.Document.URI)
277-
.Display_Base_Name
275+
& Self.File_Name.Display_Base_Name
278276
& " (version "
279277
& Self.Document_Version.Value'Image
280278
& ")");
@@ -349,6 +347,7 @@ package body LSP.Ada_Semantic_Diagnostics is
349347
begin
350348
Job.Project_Stamp := Handler.Get_Project_Stamp;
351349
Job.Document := Document;
350+
Job.File_Name := Handler.To_File (Document.URI);
352351
Job.Document_Version := Document.Identifier.version;
353352
Job.Ranges := Ranges;
354353
Job_Access := LSP.Server_Jobs.Server_Job_Access (Job);

source/ada/lsp-ada_semantic_diagnostics.ads

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ with LSP.Server_Jobs;
2424
with LSP.Structures;
2525
limited with LSP.Servers;
2626
private with Ada.Unchecked_Deallocation;
27+
private with GNATCOLL.VFS;
2728
private with Libadalang.Iterators;
2829
private with LSP.Client_Message_Receivers;
2930
private with LSP.Server_Messages;
@@ -63,6 +64,9 @@ private
6364
Document : LSP.Ada_Documents.Document_Access;
6465
-- The document to analyze.
6566

67+
File_Name : GNATCOLL.VFS.Virtual_File;
68+
-- Saved copy of the Document's virtual file, used for traces.
69+
6670
Document_Version : LSP.Structures.Integer_Or_Null;
6771
-- The document version at the time the job was enqueued.
6872

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
project Default is
2+
for Source_Dirs use ("src");
3+
end Default;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package Pkg is
2+
X : Integer := Does_Not_Exist;
3+
end Pkg;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
[
2+
{
3+
"comment": [
4+
"Regression test: ALS must not crash with STORAGE_ERROR when a",
5+
"semantic diagnostics background job is racing with a project reload."
6+
]
7+
},
8+
{
9+
"start": {
10+
"cmd": ["${ALS}"]
11+
}
12+
},
13+
{
14+
"send": {
15+
"request": {
16+
"jsonrpc": "2.0",
17+
"id": 0,
18+
"method": "initialize",
19+
"params": {
20+
"rootUri": "$URI{.}",
21+
"capabilities": {},
22+
"initializationOptions": {
23+
"ada": {
24+
"projectFile": "$URI{default.gpr}",
25+
"semanticDiagnostics": true
26+
}
27+
}
28+
}
29+
},
30+
"wait": []
31+
}
32+
},
33+
{
34+
"send": {
35+
"request": {
36+
"jsonrpc": "2.0",
37+
"method": "initialized",
38+
"params": {}
39+
},
40+
"wait": []
41+
}
42+
},
43+
{
44+
"comment": [
45+
"Open a file with a semantic error to schedule a Semantic_Diagnostics_Job.",
46+
"Do NOT wait for publishDiagnostics here so that the reload below",
47+
"races with the background job while Self.Document may still be",
48+
"referenced by the job."
49+
]
50+
},
51+
{
52+
"send": {
53+
"request": {
54+
"jsonrpc": "2.0",
55+
"method": "textDocument/didOpen",
56+
"params": {
57+
"textDocument": {
58+
"uri": "$URI{src/pkg.ads}",
59+
"languageId": "ada",
60+
"version": 1,
61+
"text": "package Pkg is\n X : Integer := Does_Not_Exist;\nend Pkg;\n"
62+
}
63+
}
64+
},
65+
"wait": []
66+
}
67+
},
68+
{
69+
"comment": [
70+
"Reload the project immediately without waiting for diagnostics.",
71+
"This races with the Semantic_Diagnostics_Job."
72+
]
73+
},
74+
{
75+
"send": {
76+
"request": {
77+
"jsonrpc": "2.0",
78+
"id": "reload",
79+
"method": "workspace/executeCommand",
80+
"params": {
81+
"command": "als-reload-project",
82+
"arguments": []
83+
}
84+
},
85+
"wait": [
86+
{
87+
"jsonrpc": "2.0",
88+
"id": "reload",
89+
"result": null
90+
}
91+
]
92+
}
93+
},
94+
{
95+
"comment": "ALS survived the reload; shut down cleanly."
96+
},
97+
{
98+
"send": {
99+
"request": {
100+
"jsonrpc": "2.0",
101+
"id": "shutdown",
102+
"method": "shutdown",
103+
"params": null
104+
},
105+
"wait": [{ "id": "shutdown", "result": null }]
106+
}
107+
},
108+
{
109+
"send": {
110+
"request": { "jsonrpc": "2.0", "method": "exit" },
111+
"wait": []
112+
}
113+
},
114+
{
115+
"stop": {
116+
"exit_code": 0
117+
}
118+
}
119+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 'diagnostics.semantic.reload_no_crash'

0 commit comments

Comments
 (0)