Skip to content

feat: add run_sync() with event loop selection and a uvloop extra - #20

Merged
AlexeyShalaev merged 1 commit into
masterfrom
feat/uvloop-run-sync
Aug 29, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
feat/uvloop-run-sync

Conversation

@AlexeyShalaev

Copy link
Copy Markdown
Member

Summary

Closes #19 — a sanctioned way to run the Host on uvloop.

Problem. The Host never creates a loop (run() is a coroutine; the documented entry point was asyncio.run(run(service, settings))), uvicorn's own loop= selection never applies because the entrypoints drive uvicorn inside the Host's loop, uvloop only arrived as a transitive dependency of uvicorn[standard] and nothing switched to it, and the obvious incantation — an event loop policy — is deprecated in Python 3.14.

Design.

  • run_sync(service, settings, *, loop="auto"), module-level and as Service.run_sync(): the blocking twin of run(). It creates the loop through asyncio.run(..., loop_factory=...), installs the OS signal handlers as run() does when no stop is supplied, and lets whatever the Host raises propagate, so the exit code keeps its meaning. loop is "asyncio", "uvloop" (an ImportError naming the extra when it is missing) or "auto" (uvloop when installed — uvicorn's convention for the same knob). event_loop_factory(loop) is exported for embedding.
  • uvloop is resolved lazily by dotted path, the way the observability registry loads backends, and uvloop joins the import-linter's "core is stdlib-only" forbidden list — the kernel's zero-dependency import stays enforced.
  • A uvloop extra (uvloop>=0.21), included in all and in the test group.
  • The "Service ready" log line carries event_loop=<module.Class> (uvloop.Loop, asyncio.unix_events._UnixSelectorEventLoop).
  • Docs: README, index, "Your first service", the tutorial and the project-layout mains use run_sync(service, settings); the lifecycle page gets an "Event loop" section; the installation page and the README list the extra. The other blueprints keep asyncio.run(service.run(settings)), which stays correct.
  • uv.lock: refreshed for the new extra (it also picks up the package's own version, which the lock still recorded as 0.1.0).

Rejected (in the issue): a loop field on AppSpec/settings (a process concern, not a description of the service), installing a policy (deprecated, process-global), documenting uvloop.run() only.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring / internal

Checklist

  • Tests added or updated — the factory for each name, the fallback and the install hint with uvloop absent, run_sync running the service on uvloop / on asyncio (a probe entrypoint records the running loop's implementation), Service.run_sync, a raising entrypoint propagating after cleanup, the readiness log field; the file cannot even import against master (cannot import name 'run_sync')
  • make check passes locally (ruff + mypy + lint-imports, 3 contracts kept with uvloop now forbidden in core)
  • CHANGELOG.md — generated by release-please from the feat: commit
  • Documentation updated (if the public API changed)

Verification

  • make check: clean
  • pytest -m unit --cov-fail-under=90: 648 passed, 10 skipped, coverage 93.17%
  • zensical build --clean: no issues

Related issues

Closes #19

The Host never creates a loop — servicewright.run() is a coroutine and the
documented entry point was asyncio.run(run(service, settings)) — so there was
no sanctioned way to run on uvloop: uvicorn's own loop= selection never
applies (the entrypoints drive uvicorn inside the Host's loop), uvloop only
arrived as a transitive dependency of uvicorn[standard] and nothing switched
to it, and the obvious incantation, an event loop policy, is deprecated in
Python 3.14.

run_sync(service, settings, *, loop="auto") is the blocking twin of run():
it creates the loop through asyncio.run(..., loop_factory=...) — "asyncio",
"uvloop" (an ImportError naming the extra when missing) or "auto" (uvloop
when installed) — installs the OS signal handlers as run() does, and lets
whatever the Host raises propagate. event_loop_factory() is exported for
embedding. uvloop is resolved lazily by dotted path like the observability
backends, and joins the import-linter's stdlib-only list for the kernel. A
uvloop extra is added (and to [all]); the readiness log line now carries
event_loop=<module.Class>, so which loop a service runs on is in the logs.

Closes #19
@AlexeyShalaev
AlexeyShalaev merged commit c070211 into master Aug 29, 2026
6 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the feat/uvloop-run-sync branch September 6, 2026 07:26
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.

[Feature]: no sanctioned way to run the Host on uvloop (uvicorn's loop selection never applies)

1 participant