Skip to content

fix(#1137): add transaction context manager for atomic multi-write operations (duplicate, conflicts resolved)#1271

Merged
utksh1 merged 7 commits into
utksh1:mainfrom
ionfwsrijan:duplicate/pr-1233-resolved
Jun 25, 2026
Merged

fix(#1137): add transaction context manager for atomic multi-write operations (duplicate, conflicts resolved)#1271
utksh1 merged 7 commits into
utksh1:mainfrom
ionfwsrijan:duplicate/pr-1233-resolved

Conversation

@ionfwsrijan

@ionfwsrijan ionfwsrijan commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Duplicate of #1233 with merge conflicts resolved and rebased onto latest main.

Problem

Every database write was auto-committed immediately (execute() calls
connection.commit() after every query). Multi-write operations (findings insert +
report insert, asset services delete+insert, task delete cascade) had no atomicity —
a crash midway would leave partial data.

Solution

  • Added Database.transaction() async context manager that wraps
    BEGIN/COMMIT/ROLLBACK
  • Wrapped the following multi-write operations in transactions:
    • _upsert_findings_and_report (findings + report + result resources)
    • _upsert_findings_and_report_from_scanner (same)
    • cancel_task (status update + audit log)
    • replace_asset_services (DELETE + INSERT loop)
    • delete_task_records (replaced manual begin()/commit()/rollback())
    • upsert_vault_secret (SELECT-then-UPDATE/INSERT race)
  • Single-statement execute() retains its auto-commit default; callers that need
    atomicity opt in via async with db.transaction():

Files changed

backend/secuscan/database.py, backend/secuscan/executor.py,
backend/secuscan/platform_resources.py, backend/secuscan/routes.py

Closes #1137

ionfwsrijan and others added 7 commits June 24, 2026 20:51
…ite operations

- Add Database.transaction() async context manager (begin/commit/rollback)
- Wrap _upsert_findings_and_report in transaction (findings + report + resources)
- Wrap _upsert_findings_and_report_from_scanner in transaction
- Wrap cancel_task status update + audit log in transaction
- Wrap replace_asset_services delete+insert in transaction
- Wrap delete_task_records in transaction (replacing manual begin/commit/rollback)
- Wrap upsert_vault_secret select-then-upsert in transaction
- Keep existing execute() auto-commit behavior for backward compatibility
- Fix indentation in replace_asset_services for loop body after
  wrapping in async with db.transaction()
- Run ruff format on all 4 changed files
… preserve rate-limit headers in 429 handler
@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@utksh1 Please review this

@utksh1 utksh1 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work. This PR adds a proper transaction() async context manager (BEGIN/COMMIT/ROLLBACK) to the database layer and wraps the executor's previously non-atomic multi-write operations (task cancellation, findings+report upsert) so they now commit atomically — directly addressing #1137. I verified all 28 tests pass (test_bulk_delete_sqlite_limit + test_process_tree_cancellation).

Notably, this also fixes a critical bug currently on main: commit c5d1967 introduced os.environ.get() calls in config.py (scan_rate_limit/window/burst) without importing os, which breaks the entire backend import. This PR adds the missing import os. Confirmed this resolves the NameError: name 'os' is not defined that currently breaks all backend tests.

LGTM, merging.

@utksh1 utksh1 merged commit c9bbb7f into utksh1:main Jun 25, 2026
20 checks passed
@utksh1 utksh1 added the gssoc:approved Admin validation: approved for GSSoC scoring label Jun 25, 2026
@ionfwsrijan

Copy link
Copy Markdown
Contributor Author

@utksh1 Please add labels

@utksh1 utksh1 added level:intermediate 35 pts difficulty label for moderate contributor PRs type:bug Bug fix work category bonus label area:backend Backend API, database, or service work labels Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend Backend API, database, or service work gssoc:approved Admin validation: approved for GSSoC scoring level:intermediate 35 pts difficulty label for moderate contributor PRs type:bug Bug fix work category bonus label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] No Transaction Isolation — Auto-Commit Per Write Operation Causes Database Inconsistency

2 participants