3223 Commits

Author SHA1 Message Date
Benoit Chesneau
086dadfa1e testing: Pin gevent and eventlet minimum versions 2026-01-23 00:26:35 +01:00
Benoit Chesneau
4062a82ba7 eventlet: Require eventlet 0.40.3+ for security fixes
Upgrade minimum eventlet version to 0.40.3 to address security
vulnerabilities:

- CVE-2021-21419 (Moderate 6.9): Websocket memory exhaustion via
  large/compressed frames (fixed in 0.31.0)
- CVE-2025-58068 (Moderate 6.3): HTTP Request Smuggling via improper
  trailer handling (fixed in 0.40.3)

Also restructure module to call monkey_patch() at import time for
better patching coverage, while keeping hubs.use_hub() in the worker's
patch() method since it creates OS resources that don't survive fork.

Add comprehensive tests for the eventlet worker.
2026-01-23 00:25:50 +01:00
Benoit Chesneau
543854c123 gevent: Require gevent 23.9.0+ for security fixes
Address CVE-2023-41419 (Critical - remote privilege escalation via
WSGIServer) by requiring gevent 23.9.0 or higher.

Changes:
- Update minimum gevent version from 1.4.0 to 23.9.0
- Remove legacy server.kill() code path (gevent < 1.0)
- Update documentation to reflect new version requirement
- Add comprehensive tests for gevent worker
2026-01-23 00:14:11 +01:00
Benoit Chesneau
4b9d787c93 tornado: Require Tornado 6.5.0+ for security fixes
Update minimum Tornado version to 6.5.0 to address:
- CVE-2024-52804 (Medium): HTTP Cookie Parsing DoS
- CVE-2025-47287 (High 7.5): Multipart/Form-Data Parser DoS

This simplifies the tornado worker by removing legacy code paths
for Tornado < 5.0 and < 6.0, reducing the codebase by ~30%.

Changes:
- pyproject.toml: Update tornado requirement to >=6.5.0
- gtornado.py: Remove TORNADO5 constant and legacy code paths
- tornadoapp.py: Update example to use async/await syntax
- test_gtornado.py: Add comprehensive test suite
2026-01-23 00:02:01 +01:00
Benoit Chesneau
5b50487bab
Merge pull request #3444 from benoitc/asgi-worker
Add native ASGI worker and uWSGI binary protocol support
2026-01-22 20:31:23 +01:00
Benoit Chesneau
81b653457c ci: Fix test dependencies for Docker and FreeBSD workflows
- Docker integration: Install pytest-cov to support coverage addopts
- FreeBSD: Install pytest-asyncio for ASGI async test support
2026-01-22 19:33:57 +01:00
Benoit Chesneau
1521266e2f asgi/uwsgi: Address PR review feedback
- asgi: Check HTTP method is GET for WebSocket upgrade per RFC 6455
  Section 4.1. Previously HEAD and other methods with upgrade headers
  could trigger WebSocket handling.

- uwsgi: Add detailed docstring explaining header mapping from CGI-style
  environment variables to HTTP headers, including the lossy nature of
  underscore-to-hyphen conversion.
2026-01-22 19:28:11 +01:00
Benoit Chesneau
99ffa0cc6b tests: Exclude docker tests from regular pytest runs
- Add tests/docker to norecursedirs in pyproject.toml to prevent
  docker tests from running during regular test suite (they require
  docker and the requests library)
- Add -p no:cov to docker integration workflow to disable coverage
  plugin since pytest-cov is not installed in that environment
2026-01-22 19:24:30 +01:00
Benoit Chesneau
ecc471f3b4 tests: Add Docker integration tests for uWSGI protocol with nginx
Add comprehensive integration tests verifying gunicorn's uWSGI binary
protocol works correctly with nginx's uwsgi_pass directive.

Test categories:
- Basic GET/POST requests with query strings and large bodies
- Header preservation (custom headers, Host, Content-Type)
- HTTP keep-alive connections
- Error responses (400-503 status codes)
- WSGI environ variables
- Large response streaming (1MB)
- Concurrent request handling
- Edge cases (binary data, unicode, long headers)

Architecture: pytest -> nginx:8080 -> uwsgi_pass -> gunicorn:8000

Also adds GitHub Actions workflow that runs on changes to uwsgi module
or docker test files.
2026-01-22 19:06:30 +01:00
Benoit Chesneau
ac7296ec49 uwsgi: Add native uWSGI binary protocol support
Add support for the uWSGI binary protocol, enabling gunicorn to work
with nginx's uwsgi_pass directive.

New module gunicorn/uwsgi/ with:
- UWSGIRequest: Parses 4-byte binary header and key-value vars block
- UWSGIParser: Protocol parser following existing Parser pattern
- Error classes: InvalidUWSGIHeader, UnsupportedModifier, ForbiddenUWSGIRequest

New configuration options:
- --protocol: Select 'http' (default) or 'uwsgi' protocol
- --uwsgi-allow-from: IP allowlist for uWSGI requests (default: localhost)

Worker integration via get_parser() factory in gunicorn/http/__init__.py,
updates to sync, gthread, and base_async workers.

Example nginx config:
    upstream gunicorn {
        server 127.0.0.1:8000;
    }
    location / {
        uwsgi_pass gunicorn;
        include uwsgi_params;
    }
2026-01-22 18:32:17 +01:00
Benoit Chesneau
903a1fdf3c tests: Add pytest-asyncio for ASGI worker tests
The ASGI worker tests use @pytest.mark.asyncio decorator which
requires the pytest-asyncio plugin to be installed.
2026-01-22 18:32:17 +01:00
Benoit Chesneau
11c6a97c47 asgi: Fix pylint and pycodestyle warnings
- Remove unused imports (ssl, os, base64, hashlib, traceback)
- Remove unused variables (body_parts, has_content_length, etc.)
- Fix no-else-break patterns in protocol.py and websocket.py
- Replace __anext__() with anext() builtin
- Remove unnecessary pass statements
- Add proper access logging to ASGI protocol handler
- Add ASGIResponseInfo class and _build_environ method for logging
- Disable too-many-return-statements for _read_frame method
- Fix raising-bad-type error (use 'is not None' check)
- Fix whitespace before colon in message.py
2026-01-22 18:03:14 +01:00
Benoit Chesneau
ae1eea8108 asgi: Add native ASGI worker with HTTP and WebSocket support
Add a new ASGI worker type that provides native async support using
gunicorn's own HTTP parsing infrastructure adapted for asyncio.

Features:
- HTTP/1.1 with keepalive support
- WebSocket connections (RFC 6455)
- ASGI lifespan protocol for startup/shutdown hooks
- Optional uvloop support for improved performance
- Full proxy protocol support (inherited from gunicorn)

New configuration options:
- --asgi-loop: Event loop selection (auto/asyncio/uvloop)
- --asgi-lifespan: Lifespan protocol control (auto/on/off)
- --root-path: ASGI root path for reverse proxy setups

Usage: gunicorn -k asgi myapp:app
2026-01-22 17:05:29 +01:00
Benoit Chesneau
ea98400820
ci: Fix macOS tests and add FreeBSD support (#3442)
* ci: Remove failing macos-13 from test matrix

* ci: Add FreeBSD testing workflow

* ci: Document test matrix rationale

* ci: Update cross-platform-actions to v0.32.0 for FreeBSD 14.2 support

* ci: Use FreeBSD 14.1 (14.2 has SSH connectivity issues)

* ci: Switch to vmactions/freebsd-vm for FreeBSD testing

* ci: Fix FreeBSD package names (pip included in Python)

* ci: Simplify FreeBSD matrix and fix package names

* ci: Use specific Python version command on FreeBSD

* ci: Add sqlite3 package for FreeBSD

* tests: Increase signal integration test timeouts for CI

The signal integration tests were flaky in CI environments,
especially FreeBSD VMs, due to 10-second timeouts being too short.
Increased timeouts to 30 seconds to handle slower CI environments.
2026-01-22 14:00:02 +01:00
Benoit Chesneau
6f103ba9b2 THANKS: Add contributors from signal handling PRs 2026-01-22 12:01:50 +01:00
Benoit Chesneau
b650332c70
Arbiter signal handling improvements (#3441)
* tests: Add tests for current signal handling behavior

Add tests for arbiter signal handling:
- TestSignalHandlerRegistration (4 tests): Verify signal handler
  registration, pipe creation, SIGCHLD separate handler, and
  expected signals list
- TestSignalQueue (4 tests): Test signal queueing, max queue size,
  wakeup writes to pipe, and sleep returns on pipe data
- TestReapWorkers (6 tests): Test worker reaping for normal exit,
  error exit codes, WORKER_BOOT_ERROR, APP_LOAD_ERROR, signal
  termination, and SIGKILL OOM hint

These tests establish baseline coverage before refactoring the
signal handling code for safety and reliability improvements.

* tests: Add tests for SIGHUP reload and worker lifecycle

Add tests for reload and worker management:
- TestSighupReload (3 tests): Verify reload spawns configured number
  of workers, calls manage_workers, and logs hang up message
- TestWorkerLifecycle (4 tests): Test spawn_worker adds to WORKERS
  dict, kill_worker sends correct signal, murder_workers sends
  SIGABRT first then SIGKILL on subsequent timeout

* arbiter: Fix waitpid status parsing using POSIX macros

Use os.WIFEXITED/WEXITSTATUS and os.WIFSIGNALED/WTERMSIG instead
of manual bit shifting for waitpid status interpretation. This
correctly distinguishes between normal exits and signal termination.

The previous code used 'status >> 8' which only worked for normal
exits, and used raw status values for signal detection which was
incorrect.

Fixes part of #3435 and #3056 (signal name display issues)

* arbiter: Change SIGTERM log level to warning

Log signal termination at warning level for expected signals
(SIGTERM, SIGQUIT) since these typically occur during normal
graceful shutdown. SIGKILL remains at error level with the
OOM hint since it indicates abnormal termination.

Fixes #3311, #3050 (SIGTERM logged as error)

* arbiter: Remove logging from SIGCHLD signal handler

Move reap_workers() call from signal handler context to main loop.
The signal handler (now signal_chld) only queues the signal and
wakes up the main loop. The actual reap_workers() is called from
handle_chld() in the main loop where logging is safe.

This fixes potential deadlocks caused by logging from signal
handler context when holding the logging lock.

Fixes #3198, #3004 (logging in signal handlers unsafe, deadlock)

* arbiter: Replace PIPE+select with queue.SimpleQueue

Use queue.SimpleQueue for signal handling instead of PIPE+select.
SimpleQueue is reentrant-safe and can be used from signal handlers.

Changes:
- Remove PIPE-based wakeup mechanism
- Add SIG_QUEUE as SimpleQueue instance
- Add WAKEUP_REQUEST sentinel for non-signal wakeups
- Replace sleep() with wait_for_signals() using queue.get()
- Simplify signal handler to just put_nowait()
- Update main loop to iterate over wait_for_signals()
- Add reap_workers() call in stop() to properly clean up workers
  since SIGCHLD is no longer processed during shutdown

This simplifies the code and removes the dependency on select().

Also adds integration tests for signal handling that verify:
- Basic request/response
- Graceful shutdown with SIGTERM/SIGINT
- SIGHUP reload
- Multiple concurrent requests

* arbiter: Wait for old workers on SIGHUP reload

After spawning new workers during reload, wait for old workers to
terminate before returning from reload(). This prevents the issue
where old workers could receive double SIGTERM - once from
manage_workers() and again from the arbiter loop.

The reload now tracks worker_age before spawning, then waits up to
graceful_timeout for workers older than that age to exit.

Fixes #3312, #3274 (SIGHUP can send double SIGTERM)

* arbiter: Log SIGCHLD at debug level

SIGCHLD is received frequently (whenever a worker exits) and doesn't
need to be logged at info level. Log it at debug level to reduce
noise in the logs while still making it available for debugging.

* tests: Fix lint warnings in test_arbiter.py
2026-01-22 11:56:23 +01:00
Benoit Chesneau
7af8cccb4a
Merge pull request #3438 from benoitc/dependabot/github_actions/actions/checkout-6
Bump actions/checkout from 5 to 6
2026-01-22 10:00:34 +01:00
Benoit Chesneau
6df99ce99b
Merge pull request #3440 from benoitc/gthread-improvements
gthread: Lock-free refactoring with PollableMethodQueue
2026-01-22 09:56:45 +01:00
Benoit Chesneau
2d03d8e6a9 tests: Add signal handling and liveness tests for gthread worker
Add tests for:
- Worker liveness reporting to arbiter via WorkerTmp
- SIGTERM graceful shutdown behavior
- SIGQUIT immediate shutdown behavior
- Worker-arbiter integration (parent death detection, timeout)
- Signal interaction edge cases (multiple signals, ordering)

These tests ensure the gthread worker properly:
- Calls notify() in the main loop for arbiter heartbeat
- Handles SIGTERM by setting alive=False and waking the poller
- Handles SIGQUIT by immediately shutting down the thread pool
- Drains connections during graceful shutdown within timeout
- Cleans up resources properly on exit
2026-01-22 09:54:04 +01:00
Benoit Chesneau
3ebf94c33f THANKS: Add contributors from gthread improvements PRs
Add contributors from the incorporated PRs:
- Dominik Działak (PR #3308)
- Oliver Allen (PR #3303)
- sylt (PR #3157)
2026-01-22 09:38:41 +01:00
Benoit Chesneau
0186211400 gthread: Lock-free PollableMethodQueue refactoring
Replace RLock-based synchronization with a pipe-based method queue
for lock-free coordination between worker threads and main thread.

Key changes:
- Add PollableMethodQueue class using os.pipe() for wake-up signaling
- Non-blocking pipe (both ends) for BSD compatibility (FreeBSD, OpenBSD)
- Unified event loop using single poller.select() - no more futures.wait()
- Better graceful shutdown with connection draining within grace period
- Rename _keep to keepalived_conns, remove _lock entirely
- Add handle_exit() for SIGTERM, improve handle_quit() for SIGQUIT
- Add set_accept_enabled() for dynamic connection acceptance control
- Add wait_for_and_dispatch_events() with EINTR handling

Performance improvement: ~8% at high concurrency due to reduced
lock contention and non-blocking pipe operations.

Tests: 40 tests covering PollableMethodQueue, graceful shutdown,
keepalive management, error handling, and BSD compatibility.

Fixes #3146
Closes #3157
2026-01-22 09:32:48 +01:00
Benoit Chesneau
b43dc6d398 gthread: Improve reliability and fix edge cases
This commit addresses three issues with the gthread worker:

1. Request body handling on keepalive
   - Add finish_body() method to Parser to discard unread body bytes
   - Call it before returning connections to the poller
   - Prevents socket appearing readable due to leftover body
   Fixes #3301

2. Timeout reliability with monotonic clock
   - Replace time.time() with time.monotonic() in set_timeout()
   - Replace time.time() with time.monotonic() in murder_keepalived()
   - Prevents timeout issues caused by NTP adjustments

3. SSL error handling
   - Move conn.init() from enqueue_req() to handle()
   - SSL handshake now runs in worker thread, not main thread
   - ENOTCONN errors during ssl_wrap_socket are caught per-connection
   - Prevents entire worker crashes on SSL handshake failures

Also adds comprehensive unit tests for the gthread worker.

Closes #3303
Closes #3308
2026-01-22 09:14:19 +01:00
dependabot[bot]
e56bed9d72
Bump actions/checkout from 5 to 6
Bumps [actions/checkout](https://github.com/actions/checkout) from 5 to 6.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-12-01 23:48:36 +00:00
Benoit Chesneau
56b5ad87f8
Restore Python 3.10 and 3.11 support (#3425)
This change extends Python support back to 3.10 and 3.11, which are
still actively maintained by the PSF:
- Python 3.10: Security support until Oct 2026
- Python 3.11: Active support (latest feature release)
- Python 3.12: Active support
- Python 3.13: Latest stable release

The previous change to support only 3.12+ was too restrictive as many
users are still on Python 3.10 and 3.11 in production environments.

Changes:
- Updated pyproject.toml to set minimum Python to 3.10
- Added Python 3.10, 3.11, and PyPy 3.10 to CI matrix
- Updated all documentation to reflect Python 3.10+ requirement
- Maintained compatibility with latest pylint for Python 3.12+
2025-10-05 22:45:21 +02:00
Benoit Chesneau
1dc4ce9d59
Update to support only Python 3.12 and 3.13 (#3422)
* Update CI and project to support only Python N (3.13) and N-1 (3.12)

- Update GitHub Actions workflows to test only Python 3.12 and 3.13
- Update pyproject.toml to require Python >= 3.12
- Update tox.ini to test only py312 and py313
- Update documentation to reflect Python 3.12+ requirement
- Clean up AppVeyor configuration for Python 3.12
* Update pylint to 3.3.2 for Python 3.12 compatibility
* Disable new pylint warnings for pre-existing issues
2025-10-05 19:55:32 +02:00
Benoit Chesneau
3f92e410d9
Merge pull request #3414 from benoitc/dependabot/github_actions/actions/checkout-5
Bump actions/checkout from 4 to 5
2025-10-05 18:23:20 +02:00
Benoit Chesneau
56927e21c4
Merge pull request #3421 from benoitc/dependabot/github_actions/actions/setup-python-6
Bump actions/setup-python from 5 to 6
2025-10-05 18:22:58 +02:00
Benoit Chesneau
e690fb9828
Merge pull request #3391 from SomeAB/patch-1
Updated IRC Channel info & Slightly improved the Community Doc
2025-10-05 18:22:35 +02:00
Benoit Chesneau
eb2f81dcf8
Merge pull request #3390 from adk-swisstopo/timeout-units
Specify the units for `graceful_timeout`.
2025-10-05 18:21:01 +02:00
Benoit Chesneau
2e6134f5cc
Merge pull request #3305 from facundobatista/patch-1
Fixed typo
2025-10-05 18:20:08 +02:00
Benoit Chesneau
36a55a71a6
Merge pull request #3378 from adk-swisstopo/patch-1
Fix typo in comment.
2025-10-05 18:19:38 +02:00
Benoit Chesneau
dad4dcdf54
Merge pull request #3418 from ChrisCarini/patch-1
fix[typo]: `usedd` -> `used`
2025-10-05 18:19:01 +02:00
dependabot[bot]
974790c0d8
Bump actions/setup-python from 5 to 6
Bumps [actions/setup-python](https://github.com/actions/setup-python) from 5 to 6.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](https://github.com/actions/setup-python/compare/v5...v6)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-version: '6'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-01 21:02:10 +00:00
Chris Carini
fb16101064
fix[typo]: usedd -> used 2025-09-19 16:03:14 -07:00
dependabot[bot]
1076fa94d4
Bump actions/checkout from 4 to 5
Bumps [actions/checkout](https://github.com/actions/checkout) from 4 to 5.
- [Release notes](https://github.com/actions/checkout/releases)
- [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
- [Commits](https://github.com/actions/checkout/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/checkout
  dependency-version: '5'
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-09-02 17:06:33 +00:00
Ahmad Bilal
0ff67f6d18
Noticed a broken link in CONTRIBUTING.md
Saw and Fixed another broken link to IRC (made it consistent) in the master/CONTRIBUTING.md
2025-04-26 15:08:24 +05:30
Ahmad Bilal
561201963e
Fixed Link for Consistency
Made the link format to be more consistent with what is on community frontpage
2025-04-26 15:02:43 +05:30
Ahmad Bilal
a03c44e39a
Updated IRC Channel info & Slightly improved the Community Doc
The Community page was a little bit outdated. Specially the IRC channel info was totally wrong, causing potential help-seekers issues in finding the correct channel/network. 

Also, slightly updated the Community page text. Its not perfect, but is a noticeable improvement imho. Fixed some broken links.
2025-04-26 02:00:05 +05:30
Adrien Kunysz
dbc00fca76 Specify the units for graceful_timeout. 2025-04-16 15:58:49 +02:00
Adrien Kunysz
68f3545e5b
Fix typo in comment. 2025-04-08 15:17:38 +02:00
Benoit Chesneau
a86ea1e4e6
Merge pull request #3355 from mondwan/patch-1
Fix typo in the settings.rst
2025-03-20 21:19:12 +01:00
Mond WAN
d05b6c5425 Fix typo in the getgrnam
getgrnam is under grp module instead of pwd
2025-03-14 18:01:52 +00:00
Mond WAN
1bc351e794 Revert "Fix typo in the settings.rst"
This reverts commit 02e5f64c76db6d33ffb22448a7f35d7aececf51d.
2025-03-14 18:00:02 +00:00
Mond WAN
02e5f64c76
Fix typo in the settings.rst
getgrnam is under grp module instead of pwd
2025-03-11 11:16:17 +00:00
Benoit Chesneau
bacbf8aa51
Merge pull request #2938 from Affirm/reuse-port-fix
Fix reuse-port to balance requests across Gunicorn workers
2024-10-25 11:36:15 +02:00
Facundo Batista
0722a3be59
Fixed typo 2024-10-01 08:24:37 -03:00
Benoit Chesneau
903792f152
Merge pull request #3269 from pajod/patch-include-tox.ini-in-sdist
packaging: include tox.ini in sdist
2024-08-13 21:52:07 +02:00
Paul J. Dorn
0c722b6915 packaging: add .pylintrc to sdist
undo when .pylintrc is moved into pyproject.toml (which is only
supported since pylint 2.5.3+ @ 2020-06-8)
2024-08-12 16:00:56 +02:00
Paul J. Dorn
81c1fc952a packaging: include tox.ini in sdist
tox testing works without full git repo, so no obvious harm in
aiding people running it from source distribution

based on setuptools itself doing it, and suggesting so in an example in
docs:
https://setuptools.pypa.io/en/latest/userguide/miscellaneous.html#controlling-files-in-the-distribution
551eb7f444

beware of .gitattributes / MANIFEST.in precedence when using setuptools-scm
2024-08-12 15:45:52 +02:00
benoitc
7268a61099 fix util.set_owner_process
Python 2.x is not supported anymore, let's remove this extra feature.

fix #3212
2024-08-11 09:52:51 +02:00