Crashes when Calling Sync on a DispatchQueue in a Transaction with a Statement Inside

Open
#929 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
30/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
sqlite, swift
Domain
databases

Research direction

Start by reproducing the crash with a Connection transaction, an external DispatchQueue.sync call, and a statement inside the closure. Trace the transaction and connection execution paths that inspect the current DispatchQueue context. Done means the example no longer crashes while preserving the existing transaction behavior.

Written by the indexing model from the issue text.

Description

bug

If you call sync on a DispatchQueue (and run a statement inside the closure) inside of a transaction, SQLite.swift will have called sync twice on the same DispatchQueue and therefore crash the program.

Internally, SQLite.swift checks the current executing DispatchQueue to find if it contains a context value made out of the bits of the Connection to figure out if it should call sync on it or not. Sync being called on a DispatchQueue changes out the queue associated with the current execution context, but continues running code on the same call stack.

func test() {
    let connection = try! Connection()
    
    let dispatchQueue = DispatchQueue(label: "Test")
    
    try! connection.transaction {
        dispatchQueue.sync {
            // Crash
            try! connection.execute(".")
        }
    }
    
    let wait = 1 + 1
}
Dominant language
Swift
Stars
10.2k
Forks
1.6k
Avg merge
6d 2h
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stephencelis/SQLite.swift

All issues in stephencelis/SQLite.swift

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.