Jump to solution
Verify

The Fix

Upgrade to version 1.0.3 or later.

Based on closed pallets/flask issue #2926 · PR/commit linked

Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.

Jump to Verify Open PR/Commit
@@ -42,6 +42,8 @@ Unreleased - Allow customizing the :attr:`Flask.url_map_class` used for routing. :pr:`3069` +- The development server port can be set to 0, which tells the OS to + pick an available port. :issue:`2926`
repro.py
>> app.run(port=0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.../lib/python3.6/site-packages/flask/app.py", line 943, in run run_simple(host, port, self, **options) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple inner() File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner fd=fd) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server passthrough_errors, ssl_context, fd=fd) File ".../lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__ self.address_family), handler) File "/.../lib/python3.6/socketserver.py", line 453, in __init__ self.server_bind() File "/.../lib/python3.6/http/server.py", line 136, in server_bind socketserver.TCPServer.server_bind(self) File "/.../lib/python3.6/socketserver.py", line 467, in server_bind self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\nUpgrade to version 1.0.3 or later.\nWhen NOT to use: This fix should not be applied if the application relies on a specific port for functionality.\n\n

Why This Fix Works in Production

  • Trigger: >> app.run(port=0)
  • Mechanism: Flask incorrectly sets the port to 5000 instead of allowing the OS to assign a port when port=0 is specified
  • Why the fix works: Fixes the issue where setting the port to 0 in Flask would default to port 5000, causing conflicts. (first fixed release: 1.0.3).
Production impact:
  • If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.

Why This Breaks in Prod

  • Shows up under Python 3.6 in real deployments (not just unit tests).
  • Flask incorrectly sets the port to 5000 instead of allowing the OS to assign a port when port=0 is specified
  • Surfaces as: >> app.run(port=0)

Proof / Evidence

  • GitHub issue: #2926
  • Fix PR: https://github.com/pallets/flask/pull/2928
  • First fixed release: 1.0.3
  • Reproduced locally: No (not executed)
  • Last verified: 2026-02-09
  • Confidence: 0.85
  • Did this fix it?: Yes (upstream fix exists)
  • Own content ratio: 0.36

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“Makes sense. Marking this as beginner friendly, I'd be happy to review a PR.”
@davidism · 2018-09-28 · source
“I think it's closed, it shows expected behavior”
@patiencedaur · 2018-10-13 · source
“Yeah but it as inactive for over a month. That's why I asked.”
@Man-Jain · 2018-11-05 · source
“@Man-Jain you can politely ask the PR implementor to see if he can fix the PR comments otherwise you can fork his PR and try…”
@theodesp · 2018-12-01 · source

Failure Signature (Search String)

  • >> app.run(port=0)

Error Message

Stack trace
error.txt
Error Message ------------- >> app.run(port=0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.../lib/python3.6/site-packages/flask/app.py", line 943, in run run_simple(host, port, self, **options) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple inner() File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner fd=fd) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server passthrough_errors, ssl_context, fd=fd) File ".../lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__ self.address_family), handler) File "/.../lib/python3.6/socketserver.py", line 453, in __init__ self.server_bind() File "/.../lib/python3.6/http/server.py", line 136, in server_bind socketserver.TCPServer.server_bind(self) File "/.../lib/python3.6/socketserver.py", line 467, in server_bind self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use

Minimal Reproduction

repro.py
>> app.run(port=0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/.../lib/python3.6/site-packages/flask/app.py", line 943, in run run_simple(host, port, self, **options) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 814, in run_simple inner() File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 774, in inner fd=fd) File "/.../lib/python3.6/site-packages/werkzeug/serving.py", line 660, in make_server passthrough_errors, ssl_context, fd=fd) File ".../lib/python3.6/site-packages/werkzeug/serving.py", line 577, in __init__ self.address_family), handler) File "/.../lib/python3.6/socketserver.py", line 453, in __init__ self.server_bind() File "/.../lib/python3.6/http/server.py", line 136, in server_bind socketserver.TCPServer.server_bind(self) File "/.../lib/python3.6/socketserver.py", line 467, in server_bind self.socket.bind(self.server_address) OSError: [Errno 98] Address already in use

Environment

  • Python: 3.6

What Broke

Setting port=0 leads to port conflicts, causing application startup failures.

Why It Broke

Flask incorrectly sets the port to 5000 instead of allowing the OS to assign a port when port=0 is specified

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

Upgrade to version 1.0.3 or later.

When NOT to use: This fix should not be applied if the application relies on a specific port for functionality.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/pallets/flask/pull/2928

First fixed release: 1.0.3

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be applied if the application relies on a specific port for functionality.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a TLS smoke test that performs a real handshake in CI (include CA bundle validation and hostname checks).
  • Alert on handshake failures by error string and endpoint to catch cert/CA changes quickly.

Version Compatibility Table

VersionStatus
1.0.3 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.