fix(app): guard against possible race conditions during enqueue #8098
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
In #7724 we made a number of perf optimisations related to enqueuing. One of these optimisations included moving the enqueue logic - including expensive prep work and db writes - to a separate thread.
At the same time manual DB locking was abandoned in favor of WAL mode.
Finally, we set
check_same_thread=False
to allow multiple threads to access the connection at a given time.I think this may be the cause of #7950:
To be honest, I don't understand enough about SQLite to confidently say that this kind of race condition is actually possible. But:
NULL
values from the DB.NOT NULL
constraints for the column that is apparently returningNULL
.enqueue_batch
call is the only place I can find where we have the possibility of a race condition due to async logic. Everywhere else, all DB interaction for the queue is synchronous, as far as I can tell.This change retains the perf benefits by running the heavy enqueue prep logic in a separate thread, but moves back to the main thread for the DB write. It also uses an explicit transaction for the write.
Will just have to wait and see if this fixes the issue.
Related Issues / Discussions
QA Instructions
Queuing should still work and it should respect max queue item size limits. I've tested and its working fine. Still just as responsive as before.
I've not been able to reproduce the issue, though, so I can't test it directly.
Merge Plan
n/a
Checklist
What's New
copy (if doing a release after this PR)