3302 Commits

Author SHA1 Message Date
Benoit Chesneau
62a29bd0e1 test(dirty): add multi-app routing tests
Add tests to verify that when multiple dirty apps are configured,
messages are correctly routed to the appropriate app based on app_path.

New files:
- tests/support_dirty_apps.py: CounterApp and EchoApp test apps
- tests/dirty/test_multi_app_routing.py: 13 routing tests covering
  app loading, routing, state separation, error handling, and
  concurrent requests
2026-01-25 10:23:25 +01:00
Benoit Chesneau
0e05c824e9 feat(examples): add FastAPI embedding service with Docker testing
Add a complete example demonstrating dirty workers with sentence-transformers
for text embeddings via FastAPI:

- EmbeddingApp DirtyApp that loads and manages the ML model
- FastAPI endpoints for /embed and /health
- Docker and docker-compose configuration
- Integration tests with numpy similarity checks
- GitHub Actions CI workflow
2026-01-25 10:23:25 +01:00
Benoit Chesneau
ce2e06ceba refactor(dirty): replace per-worker locks with queues
Replace lock-based request serialization with queue-based approach:
- Each worker now has a dedicated asyncio.Queue and consumer task
- route_request() submits (request, future) to queue and awaits future
- Consumer task processes requests sequentially per worker
- No lock contention - pure async queue operations

Benefits:
- Clearer separation of concerns
- Better visibility into request backlog (queue.qsize())
- Eliminates lock contention under high concurrency

Changes:
- worker_locks dict replaced with worker_queues and worker_consumers
- Added _start_worker_consumer() to create queue and consumer per worker
- Added _execute_on_worker() for actual worker communication
- Updated _cleanup_worker() to cancel consumer tasks
- Updated stop() to cancel all consumers before shutdown

Benchmark results (4 workers, isolated):
- throughput_10ms: 333 req/s, 0 failures
- overload_10ms (200 clients): 334 req/s, 0 failures
- All tests pass with perfect round-robin distribution
2026-01-25 10:23:25 +01:00
Benoit Chesneau
56cc094b68 feat(dirty): add benchmark suite and fix arbiter concurrency
Add comprehensive benchmark suite for stress testing the dirty pool:
- dirty_bench_app.py: Configurable benchmark app with sleep/cpu/mixed/payload tasks
- dirty_benchmark.py: Main runner with isolated and integrated test modes
- dirty_bench_wsgi.py: WSGI app for HTTP integration testing
- dirty_bench_gunicorn.py: Gunicorn config for integration benchmarks

Fix arbiter concurrency issues:
- Add per-worker locks to serialize requests and prevent read conflicts
- Implement round-robin worker selection for linear throughput scaling

The benchmark suite supports:
- Quick smoke tests (--quick)
- Full isolated benchmarks (--isolated)
- Configuration sweeps (--config-sweep)
- Payload size tests (--payload-tests)
- Integration tests with wrk (--integrated)
2026-01-25 10:23:25 +01:00
Benoit Chesneau
9b0e87deb8 docs(dirty): clarify application initialization sequence 2026-01-25 10:23:25 +01:00
Benoit Chesneau
c914f336b8 docs: add dirty arbiters to navigation and changelog 2026-01-25 10:23:25 +01:00
Benoit Chesneau
3d9382b07c refactor: address PR review comments
1. Split respawning logic from reap_dirty_arbiter() into manage_dirty_arbiter()
   to avoid respawning during shutdown/re-exec (follows reap_workers pattern)

2. Reduce public API surface in __all__:
   - Keep errors, DirtyApp, client functions as public
   - Internal protocol helpers remain importable from submodules
   - DirtyArbiter and set_dirty_socket_path kept for gunicorn core
2026-01-25 10:21:18 +01:00
Benoit Chesneau
06aba09251 feat(dirty): add thread pool with execution timeout control
- Use dirty_threads config for thread pool size (default: 1)
- Enforce dirty_timeout at worker level via asyncio.wait_for
- Heartbeat runs independently, not blocked by task execution
- Document thread safety and state persistence in docstrings
2026-01-25 10:21:18 +01:00
Benoit Chesneau
21f769ce16 fix: resolve lint issues in dirty arbiter modules 2026-01-25 10:21:18 +01:00
Benoit Chesneau
77222b8017 feat: add dirty arbiters for long-running blocking operations
Introduce Dirty Arbiters - a separate process pool for executing
long-running, blocking operations (AI model loading, heavy computation)
without blocking HTTP workers. Inspired by Erlang's dirty schedulers.

Key features:
- Completely separate from HTTP workers - can be killed/restarted independently
- Stateful - loaded resources persist in dirty worker memory
- Message-passing IPC via Unix sockets with JSON serialization
- Explicit execute() API from HTTP workers
- Asyncio-based for clean concurrent handling

Architecture:
- DirtyArbiter: manages the dirty worker pool, routes requests
- DirtyWorker: executes functions, maintains state, handles requests
- DirtyClient: sync/async API for HTTP workers to call dirty apps
- DirtyProtocol: length-prefixed JSON messages over Unix sockets
- DirtyApp: base class for dirty applications

Configuration options:
- dirty_apps: list of import paths for dirty applications
- dirty_workers: number of dirty workers (default: 0)
- dirty_timeout: task timeout in seconds (default: 300)
- dirty_graceful_timeout: shutdown timeout (default: 30)

Lifecycle hooks:
- on_dirty_starting(arbiter)
- dirty_post_fork(arbiter, worker)
- dirty_worker_init(worker)
- dirty_worker_exit(arbiter, worker)

Includes comprehensive test suite with 164 tests covering:
- Protocol encoding/decoding
- Worker and arbiter lifecycle
- Client sync/async APIs
- Signal handling
- Error handling and timeouts
- Integration tests
2026-01-25 10:21:18 +01:00
Benoit Chesneau
be6f3b97ab Disable setproctitle on macOS to prevent segfaults
setproctitle causes segfaults on macOS due to fork() safety issues
introduced in newer macOS versions. The mere import of setproctitle
can trigger crashes in forked worker processes.

Fixes #3021
2026-01-25 09:57:20 +01:00
Paul J. Dorn
481dbf2e9b
Publish full exception when the application fails to load (#3462)
* Python3: refactor returned traceback

Exceptions provide __traceback__ reference since Python 3.0
(and creating cyclic references has not been big deal since Python 2.2)

* --reload: publish entire exception, not just traceback

This is dangerous insofar as the exception text is more
likely to contain secrets than the quoted lines from traceback are.

However, the difference between the two is minor compared to the
primary danger of enabling this on a production machine, so focus
on that instead!
2026-01-25 09:41:39 +01:00
Benoit Chesneau
98156f9ef6
Merge pull request #3463 from pajod/patch-100-continue
Restrict 100 Continue resonses to reasonable count (1) & situation (HTTP/1.1)
2026-01-25 09:37:49 +01:00
Paul J. Dorn
6d61afab3e forgotten import 2026-01-25 04:11:04 +01:00
Paul J. Dorn
ba336daabe duplicate 100-continue patch for asgi 2026-01-25 03:47:49 +01:00
Paul J. Dorn
88d503ba1c HTTP/1.0 - ignore Expect: 100-continue
* ignore on HTTP/1.0 (would possibly confuse a client or proxy)
* refuse requests with unknown expectations

https://datatracker.ietf.org/doc/html/rfc9110#section-10.1.1
2026-01-24 21:59:02 +01:00
Benoit Chesneau
f0952e5874 docs: add sponsors section to website homepage 2026-01-24 02:19:14 +01:00
Benoit Chesneau
375e79e95b release: bump version to 24.1.1 2026-01-24 02:13:42 +01:00
Benoit Chesneau
ad0c12de98 docs: add sponsors section to README 2026-01-24 02:08:28 +01:00
Benoit Chesneau
70200eef46 chore: add GitHub Sponsors funding configuration 2026-01-24 01:24:44 +01:00
Benoit Chesneau
6841804116 docs: remove incorrect PR reference from Docker changelog entry 2026-01-24 00:02:56 +01:00
Benoit Chesneau
abce0ca9cb docs: add 24.1.1 changelog entry for forwarded_allow_ips fix 2026-01-23 23:53:29 +01:00
Benoit Chesneau
e9a3f30a0f
fix: keep forwarded_allow_ips as strings for backward compatibility (#3459)
The CIDR network support added in 24.1.0 changed forwarded_allow_ips
and proxy_allow_ips from string lists to ipaddress.ip_network objects.
This broke external tools like uvicorn that expect strings.

This fix validates IP/CIDR format during config parsing but keeps the
string representation. Network objects are cached in Config methods
(forwarded_allow_networks() and proxy_allow_networks()) for efficient
IP checking without repeated conversions.

Also uses strict mode for ip_network validation to detect mistakes like
192.168.1.1/24 where host bits are set (should be 192.168.1.0/24).

Fixes #3458
2026-01-23 23:51:25 +01:00
Benoit Chesneau
d73ff4b1d8 docs: update main changelog with 24.1.0 2026-01-23 22:16:37 +01:00
Benoit Chesneau
53f2c31012 ci: allow docs deploy on workflow_dispatch 2026-01-23 22:14:05 +01:00
Benoit Chesneau
eab5f0b1a5 ci: trigger Docker publish on tags with or without v prefix 2026-01-23 21:54:49 +01:00
Benoit Chesneau
a20d3fb220 docs: add Docker image to 24.1.0 changelog 2026-01-23 21:49:50 +01:00
Benoit Chesneau
7ef34796ae docs: add SIGCLD fix to changelog 2026-01-23 21:26:35 +01:00
Benoit Chesneau
3179789f46 fix: handle SIGCLD alias for SIGCHLD on Linux
On Linux, SIGCLD and SIGCHLD are aliases for the same signal number (17).
The SIG_NAMES dict iteration order can map to either name, causing
"Unhandled signal: cld" errors when workers fail during boot.

Fixes #3453
2026-01-23 21:25:07 +01:00
Benoit Chesneau
a3a59b2f56 chore: update version placeholder to 24.1.0 2026-01-23 19:27:14 +01:00
Benoit Chesneau
8b86f6c36d fix: install gunicorn from source in Docker image 2026-01-23 19:26:39 +01:00
Benoit Chesneau
076bef68d3 fix: default to 0.0.0.0 instead of [::] for broader compatibility 2026-01-23 19:19:38 +01:00
Benoit Chesneau
6a83feecd6 docs: add running in background section to Docker guide 2026-01-23 19:16:40 +01:00
Benoit Chesneau
4e656d3a91 fix: use documented worker formula (2 * CPU + 1) in Docker image 2026-01-23 19:13:47 +01:00
Benoit Chesneau
469110d647 feat: add official Docker image with GHCR publishing workflow
- Add docker/Dockerfile with non-root user and configurable environment
- Add GitHub Actions workflow to build multi-platform images (amd64/arm64)
- Publish to ghcr.io/benoitc/gunicorn on version tags
- Update documentation with official image usage examples
2026-01-23 19:09:18 +01:00
Benoit Chesneau
7894d1c170 release: prepare 24.1.0
- Bump version to 24.1.0
- Add PROXY protocol v2 documentation to deploy guide
- Add 24.1.0 changelog with new features and bug fixes
- Update all docs.gunicorn.org URLs to gunicorn.org
2026-01-23 18:47:17 +01:00
Benoit Chesneau
f3190f84cc
feat: add PROXY protocol v2 support with version selection (#3451)
Extend --proxy-protocol to accept version values (off, v1, v2, auto) instead
of being boolean-only. This allows explicit control over which PROXY protocol
versions are accepted.

Changes:
- Add InvalidProxyHeader exception for v2 binary header errors
- Add validate_proxy_protocol() validator with backwards compatibility
- Update ProxyProtocol setting with nargs="?" and const="auto"
- Add PROXY v2 constants (PP_V2_SIGNATURE, PPCommand, PPFamily, PPProtocol)
- Add _parse_proxy_protocol_v1() and _parse_proxy_protocol_v2() methods
- Update both sync (message.py) and async (asgi/message.py) parsers
- Add hex escape handling in treq.py for v2 binary test data
- Add test cases for v2 TCPv4 and TCPv6

Backwards compatible: --proxy-protocol alone (or True) maps to "auto".

Closes #2912
2026-01-23 18:40:44 +01:00
Benoit Chesneau
f95ac41b8f fix: use smaller buffer in finish_body for faster timeout
Reduce buffer size from 8192 to 1024 bytes when discarding unread
body data, allowing timeouts to trigger more quickly on slow or
stalled connections.
2026-01-23 14:46:40 +01:00
Benoit Chesneau
66963367f3 fix: set socket to blocking mode on keepalive connections
On keepalive connections, finish_request() sets the socket to non-blocking
for selector registration. When the connection is reused, handle() calls
conn.init() which returns early (already initialized) without restoring
blocking mode. This caused SSLWantReadError when WSGI apps read the
request body on SSL connections.

Fix by explicitly setting blocking mode at the start of handle().

Fixes #3448
2026-01-23 14:40:40 +01:00
Benoit Chesneau
c0c4b65f0f docs: regenerate settings.md 2026-01-23 11:41:15 +01:00
Benoit Chesneau
f22cd6558e feat: add socket backlog metric (Linux only)
Add --enable-backlog-metric option to emit a gunicorn.backlog histogram
metric showing connections waiting in the socket backlog. This helps
identify worker saturation and concurrency issues.

Also distinguishes between timer (|ms) and histogram (|h) statsd metric
types per the statsd spec.

Note: Only works on Linux using TCP_INFO from getsockopt.

Closes #2407
Partially fixes #2057
2026-01-23 11:39:05 +01:00
Benoit Chesneau
e52ac46e29 feat: support CIDR networks in forwarded_allow_ips and proxy_allow_ips
Use Python's ipaddress module to support IP networks in allow lists.
Individual IP addresses are converted to /32 (IPv4) or /128 (IPv6)
networks. CIDR notation (e.g., 192.168.0.0/16) is now supported.

Fixes #1485
Closes #2390
2026-01-23 11:39:05 +01:00
Benoit Chesneau
b0d38928c8 feat: InotifyReloader now watches newly loaded modules
Refactor reloader to share code via ReloaderBase class. InotifyReloader
now calls refresh_dirs() on each event loop timeout (~1 sec) to watch
directories for dynamically loaded modules (e.g., Django dynamic imports).

Fixes #1790
Closes #1791
2026-01-23 11:39:05 +01:00
Benoit Chesneau
bbc9bba95e fix: log SIGTERM as info level, not warning
SIGTERM is expected during graceful shutdown and reload operations.
Logging it as warning level causes unnecessary noise in error logs.
SIGKILL remains at error level (suggests OOM), other signals at warning.

Closes #3094
2026-01-23 11:39:05 +01:00
Benoit Chesneau
19d07bd4af fix: print exception to stderr on worker boot failure
When a worker fails to boot, the exception is now printed to stderr
(in addition to being logged), consistent with AppImportError handling.
This makes boot failures more visible to users.

Closes #2933
2026-01-23 11:39:05 +01:00
Benoit Chesneau
56abeaf105 fix: unreader.unread() now prepends data to buffer
The unread method was incorrectly appending data to the end of the
buffer instead of prepending it to the beginning. This caused issues
when reading partial data and then unreading it.

Closes #2915
Closes #2346
2026-01-23 11:39:05 +01:00
Benoit Chesneau
8e75b3aba3 fix: prevent RecursionError when pickling Config
On Python 3.8+ with macOS, the multiprocessing module uses 'spawn' by
default which pickles objects. When pickle.load tries to read
__setstate__ before __dict__ is restored, it hits __getattr__ causing
infinite recursion. Adding a special case for 'settings' prevents this.

Closes #2401
2026-01-23 11:39:05 +01:00
Benoit Chesneau
a182066bea fix: use proper exception chaining with 'raise from' in glogging.py
Use 'raise X from e' syntax instead of just 'raise X' when wrapping
exceptions. This provides more accurate exception chaining messages
("The above exception was the direct cause of" vs "During handling of").

Closes #2360
2026-01-23 11:39:05 +01:00
Benoit Chesneau
33e5337395 docs: fix post_request hook signature description
The description incorrectly stated the callable accepts two parameters
(Worker and Request), but the signature shows four parameters including
environ and resp.

Closes #2592
2026-01-23 11:39:05 +01:00
Benoit Chesneau
7c22955837
Merge pull request #3450 from benoitc/fix/ssl-want-read-error-3448
fix: handle SSLWantReadError in finish_body() (#3448)
2026-01-23 11:17:46 +01:00