Skip to content

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented Sep 2, 2025

Otherwise, when multiple workers encounter a diagnostic simultaneously we can encounter races which lead to corrupted diagnostic data or crashes

Resolves rdar://159598539

Copy link
Contributor

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the reason for this is because there is state preserving variable in DiagnosticsEngine that affects behavior? But this only pushing the problem downstream, requires DiagnosticConsumer also having the behavior.

It is also generally required that Consumer.finishProcessing() only gets called once, so multiple diagnostics engine owning the same consumer is a bit asking for trouble. I like the idea, but I don't think this change actually fixes the problem.

@artemcm
Copy link
Contributor Author

artemcm commented Sep 2, 2025

I guess the reason for this is because there is state preserving variable in DiagnosticsEngine that affects behavior? But this only pushing the problem downstream, requires DiagnosticConsumer also having the behavior.

It is also generally required that Consumer.finishProcessing() only gets called once, so multiple diagnostics engine owning the same consumer is a bit asking for trouble. I like the idea, but I don't think this change actually fixes the problem.

The only consumer the scanner's diagnostic engine should have is the DependencyScanDiagnosticCollector, which is thread-safe with respect to adding new diagnostics, but I am taking a look at how we handle finishProcessing.

@cachemeifyoucan
Copy link
Contributor

The only consumer the scanner's diagnostic engine should have is the DependencyScanDiagnosticCollector

Maybe it is better if the worker consumer is created and added where the diagnostics engine is created? Also add comments about the requirement for all consumers to be threadSafe. Even better is create a ThreadSafeDiagnosticConsumer sub-class that has finishProcess() final that does nothing (or put the lock into this class).

@artemcm artemcm force-pushed the DepScanWorkersNoShareDiagEngine branch from edcb671 to c2319e5 Compare September 3, 2025 17:27
@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

Maybe it is better if the worker consumer is created and added where the diagnostics engine is created?

It kind of already is. In DependencyScanningTool::getDependencies we create the collector consumer immediately before creating the scanner Compiler Instance and add it to its implicitly-created diagnostic engine.

I added a pure virtual ThreadSafeDiagnosticCollector which owns the lock and has a final do-nothing finishProcessing. I also added a stress-test unit test which reliably hits the race and corruption that this PR addresses.

@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

@swift-ci smoke test

void handleDiagnostic(SourceManager &SM, const DiagnosticInfo &Info) final override;

protected:
virtual void addDiagnostic(SourceManager &SM, const DiagnosticInfo &Info) = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't know if there is a naming convention in Swift code, but in LLVM, this usually named handleDiagnosticImpl.

@artemcm artemcm force-pushed the DepScanWorkersNoShareDiagEngine branch from c2319e5 to fbbf5d3 Compare September 3, 2025 17:55
@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

@swift-ci smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

@swift-ci smoke test macOS platform

…gine

Otherwise, when multiple workers encounter a diagnostic simultaneously we can encounter races which lead to corrupted diagnostic data or crashes

Resolves rdar://159598539
@artemcm artemcm force-pushed the DepScanWorkersNoShareDiagEngine branch from fbbf5d3 to 1cd1193 Compare September 3, 2025 20:52
@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

@swift-ci smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Sep 3, 2025

@swift-ci smoke test Windows platform

@artemcm artemcm merged commit 2934394 into swiftlang:main Sep 4, 2025
3 checks passed
@artemcm artemcm deleted the DepScanWorkersNoShareDiagEngine branch September 4, 2025 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants