Skip to content

Commit

Permalink
Don't parse in parallel
Browse files Browse the repository at this point in the history
Parsing in parallel is leading to unexpected `EXC_BAD_ACCESS`es in our codebase.  By switching to `compactMap`, the crashes go away.

The specific crash is deep in the internals of `SwiftSyntax`, which leads me to believe that there's some internal global thread-unsafe state that's being shared bewteen parsing instances. Here's an example (abbreviated) stacktrace:
```
#0	0x00000001001209e4 in protocol witness for SyntaxProtocol._syntaxNode.getter in conformance Syntax ()
#1	0x0000000100121ec3 in SyntaxProtocol.raw.getter at /Users/eric_horacek/Library/Developer/Xcode/DerivedData/Sourcery-hibchsafsxkdcjaxxaylrbeyoydh/SourcePackages/checkouts/swift-syntax/Sources/SwiftSyntax/Syntax.swift:129
#2	0x0000000100b85c32 in SimpleTypeIdentifierSyntax.init(_:) at /Users/eric_horacek/Library/Developer/Xcode/DerivedData/Sourcery-hibchsafsxkdcjaxxaylrbeyoydh/SourcePackages/checkouts/swift-syntax/Sources/SwiftSyntax/gyb_generated/syntax_nodes/SyntaxTypeNodes.swift:68
#3	0x0000000100b89979 in protocol witness for SyntaxProtocol.init(_:) in conformance SimpleTypeIdentifierSyntax ()
#4	0x00000001001acab7 in TypeSyntax.as<τ_0_0>(_:) at /Users/eric_horacek/Library/Developer/Xcode/DerivedData/Sourcery-hibchsafsxkdcjaxxaylrbeyoydh/SourcePackages/checkouts/swift-syntax/Sources/SwiftSyntax/gyb_generated/SyntaxBaseNodes.swift:421
```

I was able to reproduce this in the debugger, but there was no information available with either TSAN or ASAN.

Fixes #1009.

I know that a lot of folks rely on the performance of parsing in parallel and it only seems to be crashing on our codebase, so perhaps we could put this behind an argument?
  • Loading branch information
erichoracek committed May 6, 2022
1 parent 9616fad commit ed7ff15
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sourcery/Sourcery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public class Sourcery {
// content annotated with file annotations per file path to write it to
fileprivate var fileAnnotatedContent: [Path: [String]] = [:]

private (set) var numberOfFilesThatHadToBeParsed: Int32 = 0
private (set) var numberOfFilesThatHadToBeParsed = 0
func incrementFileParsedCount() {
OSAtomicIncrement32(&numberOfFilesThatHadToBeParsed)
numberOfFilesThatHadToBeParsed += 1
}

/// Creates Sourcery processor
Expand Down Expand Up @@ -309,7 +309,7 @@ extension Sourcery {
numberOfFilesThatHadToBeParsed = 0

var lastError: Swift.Error?
let results = parserGenerator.parallelCompactMap { parser -> FileParserResult? in
let results = parserGenerator.compactMap { parser -> FileParserResult? in
do {
return try self.loadOrParse(parser: parser, cachesPath: cachesDir(sourcePath: from))
} catch {
Expand Down

0 comments on commit ed7ff15

Please sign in to comment.