ccxt icon indicating copy to clipboard operation
ccxt copied to clipboard

"ResourceWarning: unclosed transport" after Exchange.close()

Open pietrodn opened this issue 4 years ago • 2 comments

  • OS: macOS 11.3.1
  • Programming Language version: Python 3.9
  • CCXT version: CCXT Pro 0.6.90, CCXT 1.50.11

Run this with python -X dev -X tracemalloc:

import asyncio

from ccxtpro import ftx


async def main() -> None:

    client = ftx({"verbose": True})

    await client.fetch_ticker("BTC-PERP")

    await client.close()
    #await asyncio.sleep(1)

if __name__ == '__main__':
    asyncio.run(main())

It prints out:

.venv/bin/python -X dev -X tracemalloc "/Users/pd/Library/Application Support/JetBrains/PyCharm2021.1/scratches/scratch_2.py"

[...]

Response: GET https://ftx.com/api/markets/BTC-PERP 200 {'Date': 'Thu, 20 May 2021 13:19:58 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding, Accept-Encoding, Accept-Encoding', 'Cache-Control': 'public, max-age=0', 'Etag': 'W/"e9b93c03662ea7b15ff32bd585f1512e"', 'Content-Encoding': 'gzip', 'CF-Cache-Status': 'MISS', 'cf-request-id': '0a2b87dfb000000e22470c4000000001', 'Expect-CT': 'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"', 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains; preload', 'X-Content-Type-Options': 'nosniff', 'Server': 'cloudflare', 'CF-RAY': '6525dc12b8b60e22-MXP', 'alt-svc': 'h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400'} {"success":true,"result":{"name":"BTC-PERP","enabled":true,"postOnly":false,"priceIncrement":1.0,"sizeIncrement":0.0001,"minProvideSize":0.001,"last":42411.0,"bid":42399.0,"ask":42412.0,"price":42411.0,"type":"future","baseCurrency":null,"quoteCurrency":null,"underlying":"BTC","restricted":false,"highLeverageFeeExempt":true,"change1h":0.01942167632141913,"change24h":0.26872681584300584,"changeBod":0.14497448772981292,"quoteVolume24h":7698561559.9802,"volumeUsd24h":7698561559.9802}}
/Users/pd/.pyenv/versions/3.9.4/lib/python3.9/asyncio/selector_events.py:704: ResourceWarning: unclosed transport <_SelectorSocketTransport fd=9>
  _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
Object allocated at (most recent call last):
  File "/Users/pd/.pyenv/versions/3.9.4/lib/python3.9/asyncio/selector_events.py", lineno 78
    _SelectorSocketTransport(self, rawsock, ssl_protocol,

Notice that if you uncomment the sleep after the .close(), the warning disappears.

pietrodn avatar May 20 '21 13:05 pietrodn

A bit old topic, but can you please tell whether you still experience that issue or it's gone (with current ccxt version)?

ttodua avatar Jul 21 '22 13:07 ttodua

With ccxt==1.91.39 on Python 3.10.3 I still get the warning.

However I noticed that I get the warning only if I import the CCXT Pro client (from ccxtpro import ftx), but not if I import the CCXT client (from ccxt.async_support import ftx). Therefore I suspect it is a shutdown problem of CCXT Pro.

pietrodn avatar Jul 28 '22 06:07 pietrodn