Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Handle MySQL Connector/Python Pooled Sockets at App Shutdown

Connector/Python's pooled connection close() returns it for reuse rather than closing its socket. Here’s how to distinguish normal pool behavior from shutdown or version-specific problems.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If MySQL Connector/Python pooled sockets appear to stay open when your app shuts down, first distinguish a returned connection from a closed socket. Calling PooledMySQLConnection.close() returns that connection to the pool for reuse; it does not close the underlying connection. The documented MySQLConnectionPool API examined here does not list a pool-wide close or dispose method, so per-connection close() is not a documented way to drain every pooled socket.

Why does connection.close() leave a pooled socket open?

Connector/Python uses PooledMySQLConnection objects for connections obtained from its built-in pool. Oracle’s MySQL Connector/Python Developer Guide explains that calling close() on a pooled connection returns it to the pool and makes it available for later requests. In other words, close() releases a checked-out connection; it does not tear down the socket.

As an Amazon Associate I earn from qualifying purchases.

This behavior is intentional for reuse. It also means two separate lifecycle actions are involved: returning each borrowed connection after use and disposing of the pool and its underlying connections. The documented API provides the former behavior, but the current MySQLConnectionPool API reference does not list a public pool-wide close() or dispose() method.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What to do before app termination

Identify the pool and the sockets

Confirm whether the application creates a pool through mysql.connector.connect(pool_name=..., pool_size=...) or constructs an explicit MySQLConnectionPool. Also establish that the sockets you see belong to this process rather than another application or service. An idle connection still managed by a live pool is different from a connection that the application failed to return.

Release borrowed connections after work

Close cursors when their work is complete, then call close() on every checked-out pooled connection. For a pooled connection, that call returns it to the pool; it is not a socket-shutdown command. Use the documented lifecycle behavior rather than assuming that closing a borrowed connection drains the pool.

Do not call an undocumented pool method

The current API reference lists pool construction, add_connection(), get_connection(), set_config() and the pool_name property, but no pool-wide disposal entry. Avoid adding pool.close() as a proposed fix unless the official documentation for the exact Connector/Python version in deployment documents that method. Directly changing private connector attributes is not a supported substitute.

What if the requirement is to close every socket?

If shutdown requires an explicit, documented pool-wide disposal operation, the built-in pool’s documented API examined here does not establish one. Evaluate a pool manager that documents this lifecycle operation, then verify its compatibility with the MySQL driver and how it handles connections still checked out when shutdown begins. No specific alternative was validated here, so a replacement should not be treated as a proven fix without checking its API and behavior against the deployed versions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to investigate sockets that remain after shutdown

No single cause can be established without the application’s versions and shutdown details. Record the Connector/Python version, whether it uses the C extension or pure-Python implementation, the MySQL server version, how the pool is created, whether all borrowed connections are returned, and how the process terminates. Then reproduce the shutdown behavior in that deployment and inspect the socket owner and state. The available evidence does not establish exactly when a particular operating system or server releases sockets after process exit.

If the process is still running, check whether connections were returned to the pool and whether the pool is expected to keep them available for reuse. If the process has exited, distinguish application-level pool behavior from operating-system and server cleanup; the documented meaning of pooled close() alone cannot explain the observed timing.

Check version-specific close and reset failures

Historical reports show why an exception during close should be investigated separately from ordinary pool return behavior. An Oracle MySQL bug report describes an error closing pooled connections with Connector/Python 8.0.22–8.0.26, the C extension, and a MySQL server below 5.7.13. A MySQL developer response said the issue was fixed in Connector/Python 8.0.29. These are historical version-specific findings, not evidence that current releases have the same problem. See the Oracle MySQL bug report.

A separate report describes pooled connections becoming unavailable after a reset exception when the server connection was lost; it notes the behavior in the Connector/Python 2.1.6 changelog. If your shutdown produces an exception, capture the exact connector and server versions, connector mode, error, and connection-loss circumstances before applying an old report to a current installation. See the Oracle MySQL bug report.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Bottom line for the built-in pool

Return each checked-out connection with its close() method, but interpret that action as releasing the connection back to the pool. The documented API examined here does not provide a public pool-wide disposal method. If your application needs explicit socket teardown at shutdown, verify that lifecycle capability in a documented pool manager or confirm the actual process-exit behavior in your own deployment.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.