Skip to content

Migrate liveservertestcase completion - #1

Closed
fin34r wants to merge 11 commits into
deronnax:migrate-liveservertestcase-to-threadfrom
fin34r:migrate-liveservertestcase-completion
Closed

fin34r wants to merge 11 commits into
deronnax:migrate-liveservertestcase-to-threadfrom
fin34r:migrate-liveservertestcase-completion

Conversation

@fin34r

@fin34r fin34r commented Nov 12, 2026

Copy link
Copy Markdown

Continuation of django#2182 — carried onto current django/channels:main and finished. Fixes django#2181 (parallel test runs crash: daemonic processes are not allowed to have children) and fixes django#2194 (mail.outbox was empty in tests; the server now runs in a thread, so the test and the server share one process).

What this PR adds on top of the current branch — 3 commits, your 8 commits untouched (original SHAs). This branch stacks directly on your head, so it merges without conflicts.

  1. Bounded graceful shutdown (channels/testing/live.py)
  • In-flight requests and open websocket consumers are drained before the reactor stops; websockets are closed with code 1001 and the consumer's disconnect handler runs.
  • Abandoned requests (client already gone) are retired before the test database is flushed, instead of raising 500s during teardown.
  • A fresh thread-sensitive executor is installed per server thread; the asyncio loop is closed after the reactor stops; the txaio reactor is reset.
  • Teardown is hard-bounded by two new tunables, shutdown_grace_period and shutdown_cancel_timeout, so a stuck request cannot hang the test run.
  • Defensive override: kill_all_applications from daphne.testing raises an uncaught KeyError on idle sockets. We work around it here; it should also be reported upstream in the daphne repository.
  1. Regression tests (8 modules in tests/sample_project/tests/, up from 1)
  1. Docs and packaging
  • testing.rst documents the thread model, the extension points (server_thread_class, startup_timeout), the shutdown tunables, quiesce(), the one-server-per-process constraint, and one known limitation: a view that ignores cancellation inside sync_to_async(thread_sensitive=True) parks its worker thread until the process exits. Not prevented, only documented.
  • CHANGELOG notes the removal of serve_static, static_wrapper, and ProtocolServerProcess.
  • setup.cfg pins daphne>=4.2. This is the only dependency change we made; the shutdown code depends on 4.2 behavior.

Note for review: the diff also carries current django/channels:main state inside the files this PR touches (CI matrix and action version bumps, CHANGELOG.txt, python_requires, Django test versions). That is main's content, not changes made by this PR; your branch reaches the same state on its next rebase. Files outside our change set are untouched (README, version number, release notes stay at your base's versions).

Out-of-scope observations — four things we noticed but deliberately did not change in this PR, listed in case you want them handled separately:

  1. Static files: the old serve_static/static_wrapper attributes are removed with no built-in replacement beyond static_handler. Serving static files is still possible by overriding static_handler, but there is no easier escape hatch, and the removed attributes had no deprecation cycle.

  2. In-memory databases: the test database sharing between the server thread and the test worker relies on Django's thread-sharing mechanism for database connections, which does not apply to all in-memory database setups; adopters on in-memory SQLite may need explicit configuration.

  3. Reactor ownership: the test case stops and resets the Twisted reactor during startup and teardown. Code that owns its own reactor configuration around the test run needs to account for that.

  4. tox envlist: the parallel gate is a separate tox env, not part of the default envlist; a bare tox run does not execute it.

deronnax and others added 11 commits September 4, 2025 10:27
…ds (fixes django#2181)

It was previously using processes, but it was causing errors when tests were ran with
'--parallel', as it was a daemon process and daemon processes can't be spawned when
already in a subprocesses.
* Add server_class attribute
* Move application creation logic into _create_server
* Remove unnecessary static_handler None check
* Add connections_override parameter to _create_server
* remove useless print
Drain in-flight ASGI application instances before stopping the reactor, install a fresh thread-sensitive executor per server thread, retire an abandoned HTTP request before the test database flush, and close open websocket subscribers with the websocket-recommended close code plus the consumer's disconnect. The asyncio event loop is closed after the reactor stops, the txaio reactor is reset, and teardown is bounded by the new shutdown_grace_period and shutdown_cancel_timeout attributes. A last-moment re-check retires connections created between the retirement pass and reactor stop. terminate() and quiesce() tolerate a closed loop, and the executor swap is restored once it drained.
Seven live-server test modules cover thread reuse across sequential servers, a cached async_to_sync chain, graceful in-flight shutdown, abandoned in-flight requests, and open websocket subscribers (graceful 1001 close code, disconnect handler run, quiet teardown output). Each paired class is order-independent, and the modules are exercised in CI by the extended 'tox -e parallel' gate running with '--parallel 2' (tblib included for readable parallel tracebacks). Sample-project fixtures (a sync-only middleware, websocket disconnect recording, ALLOWED_HOSTS) support the tests, and the thread-less enterClassContext shim keeps Python 3.10 usable via the new helper used by the tests.
testing.rst documents the thread model, the server_thread_class and startup_timeout extension points, the shutdown grace/cancel budget and quiesce(), the one-server-per-process constraint, and the private dependencies the implementation relies on. CHANGELOG notes the graceful shutdown behaviour and the two new tunables, alongside the advisory daphne>=4.2 requirement (channels[daphne] extra; first daphne release with Python 3.13 support) and the removed serve_static/static_wrapper/ProtocolServerProcess API. setup.cfg pins daphne>=4.2 and .gitignore ignores --parallel sqlite clone files.
@deronnax
deronnax force-pushed the migrate-liveservertestcase-to-thread branch from 5bdb0fe to 5349fea Compare November 12, 2026 15:08
@fin34r

fin34r commented Nov 12, 2026

Copy link
Copy Markdown
Author

first time contributor, ai generated, manually reviewed. Happy to take on any suggestions or make any changes. hope this helps.

@deronnax

Copy link
Copy Markdown
Owner

I do have access to AI too, thank you.

@deronnax deronnax closed this Nov 12, 2026
@fin34r

fin34r commented Nov 12, 2026

Copy link
Copy Markdown
Author

I didn't think that was your limiting factor. Considering it was a days work to investigate and troubleshoot all the various gotchas, I was trying to save you time and energy. Glad to see you're on top it. Thanks for your contribution. Looking forward to it's release.

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.

2 participants