Title: asyncio loop.getaddrinfo raises RuntimeError
Type: behavior Stage: resolved
Components: asyncio Versions: Python 3.9
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, kappa, yselivanov
Priority: normal Keywords:

Created on 2020-11-25 16:57 by kappa, last changed 2020-11-28 10:54 by asvetlov. This issue is now closed.

Messages (6)
msg381850 - (view) Author: Giacomo Caironi (kappa) Date: 2020-11-25 16:57
import asyncio
import traceback
from threading import Thread

class Test(Thread):
    def __init__(self):
        self.loop = asyncio.new_event_loop()

    async def getaddrinfo(self, loop):
            print(await loop.getaddrinfo("", 8333))
        except Exception:

    def run(self):
        loop = self.loop
        asyncio.run_coroutine_threadsafe(self.getaddrinfo(loop), loop)

test = Test()

Executing the previous code throws RuntimeError("can't register atexit after shutdown") only in python3.9. Strangely it doesn't happen when I execute the code in interactive mode. 

I think this is related to
msg381884 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-11-26 08:12
Your snippet exits just after starting.
`getaddrinfo()` is called at the interpreter shutdown state.

I'm not sure that do you expect to get here.  We can put some effort and raise an error with another message but cannot make the example working.

I suggest just closing the issue; nothing to do here.
msg381893 - (view) Author: Giacomo Caironi (kappa) Date: 2020-11-26 11:40
Why do you say that `getaddrinfo()` is called at the interpreter shutdown state? On my machine it works and the output is [(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('', 8333)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('', 8333)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('', 8333)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('2a00:1450:4002:805::2004', 8333, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('2a00:1450:4002:805::2004', 8333, 0, 0)), (<AddressFamily.AF_INET6: 10>, <SocketKind.SOCK_RAW: 3>, 0, '', ('2a00:1450:4002:805::2004', 8333, 0, 0))], which is printed on the console
msg381896 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-11-26 12:13
Because you have `test.start()` and immediately finish the script.

The interpreted goes to *shutdown* state and implicitly waits for `test.join()` because the test thread is not a daemon.
msg381897 - (view) Author: Giacomo Caironi (kappa) Date: 2020-11-26 12:26
Ok but then why does it work in python3.8 and not in python 3.9?
msg381899 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2020-11-26 13:18
Because it did work in a gray area.
The finalization logic has slightly changed, some tricks that were working before now fails.
Sure, other undocumented tricks start working :)

The rule of thumb: please stop and close the asyncio loop before exiting the program. This rule allows for avoiding situations like this.
Date User Action Args
2020-11-28 10:54:28asvetlovsetstatus: open -> closed
resolution: wont fix
stage: resolved
2020-11-26 13:18:28asvetlovsetmessages: + msg381899
2020-11-26 12:26:30kappasetmessages: + msg381897
2020-11-26 12:13:04asvetlovsetmessages: + msg381896
2020-11-26 11:40:49kappasetmessages: + msg381893
2020-11-26 08:12:35asvetlovsetmessages: + msg381884
2020-11-25 16:57:54kappacreate