Thriftpy / thriftpy2

Pure python approach of Apache Thrift.
MIT License
562 stars 89 forks source link

[BUG] Asynchronous Client Hangs When Connecting to Non-Existent IP #270

Open fuergaosi233 opened 1 month ago

fuergaosi233 commented 1 month ago

Description: When using an asynchronous client to connect to a non-existent IP, the program hangs indefinitely because the socket operation is pending globally. This issue causes the entire program to be stuck.

Steps to Reproduce:

  1. Use the following code to create an asynchronous client that attempts to connect to a non-existent IP.
  2. The program will hang and not proceed beyond the client connection attempt.

Code to Reproduce:

# -*- coding: utf-8 -*-
import thriftpy2
import asyncio
from thriftpy2.rpc import make_aio_client

echo_thrift = thriftpy2.load("echo.thrift", module_name="echo_thrift")
import datetime

async def print_current_time():
    while True:
        print(datetime.datetime.now())
        await asyncio.sleep(1)

async def main():
    task_current_time = asyncio.create_task(print_current_time())
    await asyncio.sleep(5)
    client = await make_aio_client(
        echo_thrift.EchoService, "100.100.100.100", 6000, connect_timeout=100* 1000
    )
    print(await client.echo("hello, world"))
    client.close()

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Expected Behavior: The connection attempt should timeout after the specified connect_timeout duration and raise an appropriate exception, allowing the program to handle it gracefully and don't block loop.

Actual Behavior: The program hangs indefinitely and does not raise an exception after the timeout period. This behavior prevents the rest of the program from executing.

Environment:

Additional Information: The issue appears to be related to how the socket operation is handled when connecting to an unreachable IP address. This results in the entire program being stuck, which can be problematic for applications relying on asynchronous operations.