Skip to content

Commit 314da03

Browse files
authored
Merge pull request #18160 from benlangmuir/edit-test-race
[sourcekit] Fix a race between AST processing and reopening a document
2 parents ac4a92a + a8d5493 commit 314da03

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,8 +1673,13 @@ ImmutableTextSnapshotRef SwiftEditorDocument::replaceText(
16731673
}
16741674

16751675
void SwiftEditorDocument::updateSemaInfo() {
1676-
if (Impl.SemanticInfo) {
1677-
Impl.SemanticInfo->processLatestSnapshotAsync(Impl.EditableBuffer);
1676+
Impl.AccessMtx.lock();
1677+
auto EditableBuffer = Impl.EditableBuffer;
1678+
auto SemanticInfo = Impl.SemanticInfo;
1679+
Impl.AccessMtx.unlock(); // Not a recursive mutex, so unlock before processing
1680+
1681+
if (SemanticInfo) {
1682+
SemanticInfo->processLatestSnapshotAsync(EditableBuffer);
16781683
}
16791684
}
16801685

@@ -1978,6 +1983,7 @@ void SwiftEditorDocument::expandPlaceholder(unsigned Offset, unsigned Length,
19781983
}
19791984

19801985
ImmutableTextSnapshotRef SwiftEditorDocument::getLatestSnapshot() const {
1986+
llvm::sys::ScopedLock L(Impl.AccessMtx);
19811987
return Impl.EditableBuffer->getSnapshot();
19821988
}
19831989

unittests/SourceKit/SwiftLang/EditingTest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ void EditTest::doubleOpenWithDelay(useconds_t delay, bool closeDoc) {
253253
// be queried, since the semantic info from the first open is unreachable.
254254
for (int i = 0; i < 2; ++i) {
255255
bool expired = waitForDocUpdate(/*reset=*/true);
256-
ASSERT_FALSE(expired) << "no second notification";
256+
ASSERT_FALSE(expired) << "no " << (i ? "second " : "") << "notification";
257257
replaceText(DocName, 0, 0, StringRef(), Consumer);
258258
if (!Consumer.Diags.empty())
259259
break;
@@ -262,6 +262,8 @@ void EditTest::doubleOpenWithDelay(useconds_t delay, bool closeDoc) {
262262

263263
ASSERT_EQ(1u, Consumer.Diags.size());
264264
EXPECT_STREQ("use of unresolved identifier 'unknown_name'", Consumer.Diags[0].Description.c_str());
265+
266+
close(DocName);
265267
}
266268

267269
// This test is failing occassionally in CI: rdar://42483323

0 commit comments

Comments
 (0)