Skip to content

Infinite Loop when trying to connect with wrong settings (e.g. TLS) #718

@waza-ari

Description

@waza-ari

Observed behavior

There is an issue which seems to be related to #490 but maybe not the same. Imagine connecting to a server which has TLS configured but you're providing the wrong certificate. The client will get stuck in a never-terminating state, not trying to re-connect but also not trying to exec.

To recreate, configure a server with TLS, and then simply connect (reconnect is here for clarification, its actually the default.

import asyncio

import nats


async def main():
    # It is very likely that the demo server will see traffic from clients other than yours.
    # To avoid this, start your own locally and modify the example to use it.
    await nats.connect("nats://localhost:4222", allow_reconnect=True)


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

After throwing the SSL error (ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate), which is totally expected, the client hangs infinitely. I debugged the code and traced it to the transport handling.

The _close() method is called, which works fine until this part:

nats.py/nats/aio/client.py

Lines 781 to 786 in 8f53949

if self._transport is not None:
self._transport.close()
try:
await self._transport.wait_closed()
except Exception as e:
await self._error_cb(e)

The code never reaches anything after that block, it enters wait_closed in line 784 and gets stuck in there indefinitely.

Expected behavior

It should stop gracefully if the connection fails.

Server and client version

  • Client: 2.11.0
  • Server: latest docker tag

Host environment

MacOS, although I doubt it matters.

Steps to reproduce

See above

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectSuspected defect such as a bug or regression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions