Skip to content

Fix st2auth under gevent (HTTP 500 on login, TLS crash) - #6426

Open
jk464 wants to merge 2 commits into
StackStorm:masterfrom
jk464:gevent-st2auth-fix
Open

jk464 wants to merge 2 commits into
StackStorm:masterfrom
jk464:gevent-st2auth-fix

Conversation

@jk464

@jk464 jk464 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Problem

Two independent bugs make st2auth unusable under gevent.

1. HTTP 500 on POST /tokens (BlockingSwitchOutError)

concurrency.wsgi_server() always passed spawn=custom_pool to gevent's WSGIServer. st2api and st2stream always pass a pool, but st2auth does not, so gevent received spawn=None. With spawn=None gevent calls the request handler synchronously in the hub greenlet. Anything that then has to switch to the hub fails with BlockingSwitchOutError. With pymongo that is opening a new connection and resolving the host through gevent's thread resolver, so POST /tokens returned HTTP 500 whenever the connection pool needed a new connection.

2. Crash on startup with [auth] use_ssl = True

gevent's WSGIServer needs a plain listening socket and does the TLS handshake itself, per accepted connection. st2auth pre-wrapped the listening socket with concurrency.wrap_ssl(), so it crashed with TypeError: Expected a regular socket, not SSLSocket.

Changes

Two commits, one per fix:

  1. wsgi_server() only passes spawn to gevent when a pool was given, so gevent spawns a greenlet per request.
  2. wsgi_server() takes certfile and keyfile. Under eventlet it still wraps the socket with eventlet.wrap_ssl(). Under gevent it passes an ssl_context to WSGIServer. st2auth now passes the files instead of wrapping the socket. A note on wrap_ssl() says not to use it for a WSGI server's listening socket.

Both have a CHANGELOG.rst entry.

Tests

New st2common/tests/unit/test_concurrency.py, which runs wsgi_server() for plain HTTP (with and without a custom pool) and HTTPS, plus a check that a plain HTTP client does not take the TLS server down. The app handler sleeps, so it fails if the request runs in the hub greenlet.

  • Pass under gevent (the default) and with eventlet selected: 4 passed each.
  • With the fixes reverted, 3 of the 4 fail under gevent (BlockingSwitchOutError, SSL WRONG_VERSION_NUMBER).
  • The pre-commit hooks (black, flake8, pylint, bandit) pass.

Verification

Both bugs were reproduced against an st2auth 3.10dev build on Python 3.12 with gevent 26.8 and a TLS-only MongoDB, in addition to the unit tests above.

🤖 Generated with Claude Code

@pull-request-size pull-request-size Bot added the size/L PR that changes 100-499 lines. Requires some effort to review. label Dec 1, 2026
concurrency.wsgi_server() always passed spawn=custom_pool to gevent's
WSGIServer. st2auth passes no pool, so gevent received spawn=None, which makes
it call the request handler synchronously in the hub greenlet. Anything that
then has to switch to the hub fails with BlockingSwitchOutError: with pymongo
that is opening a new connection and resolving the host through gevent's thread
resolver, so POST /tokens returned HTTP 500 whenever the connection pool needed
a new connection. st2api and st2stream always pass a pool and are not affected.

Only pass spawn when a pool was given, so gevent spawns a greenlet per request.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jk464
jk464 force-pushed the gevent-st2auth-fix branch from 2d8024a to 4014c2e Compare December 1, 2026 14:18
@jk464
jk464 marked this pull request as ready for review December 1, 2026 14:24
st2auth wrapped its listening socket with concurrency.wrap_ssl() and handed the
SSLSocket to wsgi_server(). gevent's WSGIServer requires a plain listening
socket and does the TLS handshake itself, per accepted connection, so with
[auth] use_ssl = True st2auth crashed at startup with "TypeError: Expected a
regular socket, not SSLSocket".

wsgi_server() now takes certfile and keyfile. Under eventlet it still wraps the
socket with eventlet.wrap_ssl(); under gevent it passes an ssl_context to
WSGIServer. st2auth passes the files instead of wrapping the socket.

The HTTPS tests verify the server certificate: the client trusts only the fixture
CA and checks the host name, and there are negative tests for an untrusted CA
and a wrong host name (both must raise SSLCertVerificationError). They run on
both gevent and eventlet.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jk464
jk464 force-pushed the gevent-st2auth-fix branch from 4014c2e to 7ba9eec Compare December 4, 2026 12:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/L PR that changes 100-499 lines. Requires some effort to review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant