Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread-safe exception #12

Closed
petersktang opened this issue Sep 26, 2023 · 2 comments
Closed

Thread-safe exception #12

petersktang opened this issue Sep 26, 2023 · 2 comments

Comments

@petersktang
Copy link
Contributor

petersktang commented Sep 26, 2023

Description of the Exception

When running the below concurrently in a multi-thread situation, the below functions sometimes hit thread-safe exception.

  • getInterElementSpaces()
  • MTMathAtomFactory.delimValueToName
  • MTMathAtomFactory.accentValueToName
  • MTMathAtomFactory.textToLatexSymbolName

Thread 2: EXC_BAD_ACCESS (code=1, address=0xfffffffffffffff0)

Test code run repeatedly

    private let executionQueue = DispatchQueue(label: "com.swiftmath.concurrencytests", attributes: .concurrent)
    private let executionGroup = DispatchGroup()
    
    let totalCases = 20
    var testCount = 0
    
    func testSwiftMathConcurrentScript() throws {
        for caseNumber in 0 ..< totalCases {
            helperConcurrency(caseNumber, in: executionGroup, on: executionQueue) {
                let result1 = getInterElementSpaces()
                let result2 = MTMathAtomFactory.delimValueToName
                let result3 = MTMathAtomFactory.accentValueToName
                let result4 = MTMathAtomFactory.textToLatexSymbolName
                XCTAssertNotNil(result1)
                XCTAssertNotNil(result2)
                XCTAssertNotNil(result3)
                XCTAssertNotNil(result4)
            }
        }
        executionGroup.notify(queue: .main) { [weak self] in
            // print("All test cases completed: \(self?.testCount ?? 0)")
        }
        executionGroup.wait()
    }
    func helperConcurrency(_ count: Int, in group: DispatchGroup, on queue: DispatchQueue, _ testClosure: @escaping () -> (Void)) {
        let workitem = DispatchWorkItem {
            testClosure()
        }
        workitem.notify(queue: .main) { [weak self] in
            self?.testCount += 1
        }
        queue.async(group: group, execute: workitem)
    }

Further investigations

At the moment the below static function is not invoked by any code, however, if in-use, will also cause thread-safety exception.

    public static func add(latexSymbol name: String, value: MTMathAtom) {
        supportedLatexSymbols[name] = value
        Self.textToLatexSymbolName[value.nucleus] = name
    }
@petersktang
Copy link
Contributor Author

issued a push request.

@mgriebling
Copy link
Owner

Incorporated. Thanks for the fix.

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

No branches or pull requests

2 participants