Skip to content

Conversation

@jogendra-india
Copy link

Description

Fixes #259

This PR fixes the sqlite3.OperationalError: database is locked error that occurs when using --testmon --testmon-nocollect with pytest-xdist.

Problem

When --testmon-nocollect is used, TestmonXdistSync was not registered because it was inside the if should_collect: block. This meant:

  1. Workers didn't receive testmon_exec_id from the controller via pytest_configure_node hook
  2. Each worker called TestmonData.for_local_run() (write mode) instead of for_worker() (read-only)
  3. Multiple workers raced for the SQLite write lock → database locked error

Solution

  1. Move TestmonXdistSync registration to the if should_select or should_collect: block, so it's registered whenever testmon is active
  2. Add should_collect parameter to TestmonXdistSync.__init__() to track whether collection is active
  3. Skip sync_db_fs_tests() in pytest_xdist_node_collection_finished() when in nocollect mode (since DB is read-only)

Changes

testmon/pytest_testmon.py

  • Modified register_plugins() to register TestmonXdistSync in the if should_select or should_collect: block
  • Added should_collect parameter to TestmonXdistSync.__init__()
  • Updated pytest_xdist_node_collection_finished() to check self._should_collect before syncing

tests/test_xdist_integration.py (new file)

  • Added tests for xdist integration with testmon
  • Added regression test for the database lock issue

Testing

# Run the new tests
pytest tests/test_xdist_integration.py -v

# Run with tox (xdist environment)
tox -e xdist-py310

Backward Compatibility

This change is fully backward compatible:

  • should_collect parameter defaults to True
  • Existing behavior is preserved when --testmon-nocollect is not used
  • The fix only affects the xdist + nocollect combination

Checklist

  • Code changes fix the issue
  • Added test case to prevent regression
  • Existing tests pass
  • Backward compatible

Register TestmonXdistSync plugin conditionally based on xdist usage and modify its constructor to accept a should_collect parameter.
@tarpas
Copy link
Owner

tarpas commented Jan 8, 2026

I don't see the test file in the commit. (Force add it pls)

I think I would delete most of the code comments.

Add integration tests for pytest-xdist with testmon to verify proper handling of worker coordination and database locking issues.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite3.OperationalError: database is locked when using --testmon-nocollect with pytest-xdist

2 participants