Skip to content

Commit a8d5493

Browse files
committed
[sourcekit] Fix a race between AST processing and reopening a document
When a document is opened twice without closing it we re-initialize its internal data. Make sure we don't race with code that accesses that data. Found by TSan!
1 parent c2fadc3 commit a8d5493

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
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

0 commit comments

Comments
 (0)