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

Race condition between the execution of a statement and the retrieval of the last error from SQLite #1416

Closed
trueqbit opened this issue Mar 25, 2025 · 0 comments · Fixed by #1412
Assignees

Comments

@trueqbit
Copy link
Collaborator

[...] sqlite_orm is doing some thread-unsafe calls that it doesn't have to do, and could definitely use some improvement in this area. For example, there is lots of code that looks like this:

if(sqlite3_step(stmt) == SQLITE_DONE) {
    //  done..
} else {
    throw std::system_error(std::error_code(sqlite3_errcode(db), get_sqlite_error_category()),
                            sqlite3_errmsg(db));
}

which isn't thread safe because there's a race condition between the sqlite3_step(stmt) and the sqlite3_errcode(db)/sqlite3_errmsg(db) calls, which means the code and message in the exception aren't reliable when used multithreaded.

If I was writing it using raw libsqlite3, then I would write it to be thread-safe by writing it as:

auto rc = sqlite3_step(stmt);
if (rc == SQLITE_DONE) {
    // done..
} else {
    throw std::system_error(std::error_code(rc, get_sqlite_error_category()), sqlite3_errstr(rc));
}

but I can't currently get that thread safety with sqlite_orm.

Originally posted by @jagerman in #707

@trueqbit trueqbit self-assigned this Mar 25, 2025
@trueqbit trueqbit moved this to In Progress in sqlite_orm public board Mar 25, 2025
@trueqbit trueqbit moved this from In Progress to Done in sqlite_orm public board Mar 26, 2025
@trueqbit trueqbit closed this as completed by moving to Done in sqlite_orm public board Mar 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging a pull request may close this issue.

1 participant