Possible synchronisation issues

Open
#1,088 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start with the prepare(_:) implementation in QueryType.swift and trace how statement.next() is executed relative to the serial queue and caller thread. Determine whether the reported interleaving can cause the database corruption crash, then define a regression test and a fix or documented resolution for the synchronization issue.

Written by the indexing model from the issue text.

Description

Our app was facing a database corruption crash while reading from the database.
I was going through the implementation of SQLite.swift to check for possible synchronisation issues and found this piece of code.

// QueryType.swift
extension Connection {
    public func prepare(_ query: QueryType) throws -> AnySequence<Row> {
        let expression = query.expression
        let statement = try prepare(expression.template, expression.bindings)

        let columnNames = try columnNamesForQuery(query)

        return AnySequence {
            AnyIterator { statement.next().map { Row(columnNames, $0) } }
        }
    }
...

Each call to statement.next() is executed in a serial queue. But in between the two calls the code switches to the caller thread and leaves the database exposed to a sneaky write operation. This can potentially cause the subsequent statement.next() call to throw a corruption error.

@jberkel @NathanFallet Please check.

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.