eclipse-paho / eclipse-paho/paho.mqtt.python
import of paho.mqtt.client not possible when ONLY importing in subthread
- Dominant language
- Python
- Stars
- 2.4k
- Forks
- 742
- Avg merge
- 12d 48m
- Merged PRs (30d)
- 1
Description
# Bug Description
When running a python script and trying to import paho.mqtt.client in a subthread, without first importing the library in the main thread, an exception occurs. If the library is imported before in the main thread, no problems occurr.
# Reproduction
This simple code snippet:
```
import threading
def mqtt_thread():
print("Starting MQTT")
import paho.mqtt.client as mqtt
print("MQTT imported in thread")
t = threading.Thread(target=mqtt_thread)
t.start()
```
executes up to the first print statement, I see the "Starting MQTT" message, then the following exception occurrs:
**RuntimeError: can't register atexit after shutdown**
When doing the same without the subthread, there are no problems at all. Also, when first importing the package in the main thread, like here:
```
import threading
import paho.mqtt.client as mqtt
def mqtt_thread():
print("Starting MQTT")
**import paho.mqtt.client as mqtt**
print("MQTT imported in thread")
t = threading.Thread(target=mqtt_thread)
t.start()
```
also works just fine.
# Environment
* Python version: tested on 3.11.3 and 3.11.9
* Library version: 2.1.0
* Operating system: Windows 11 (tested on different versions)
# Whole Stacktrace:
Starting MQTT
Exception in thread Thread-1 (mqtt_thread):
Traceback (most recent call last):
File "C:\Python\311_x64\Lib\threading.py", line 1045, in _bootstrap_inner
self.run()
File "C:\Python\311_x64\Lib\threading.py", line 982, in run
self._target(*self._args, **self._kwargs)
File "c:\Users\KonstantinWaser\Desktop\pahotest.py", line 18, in mqtt_thread
import paho.mqtt.client as mqtt
File "C:\Python\311_x64\Lib\site-packages\paho\mqtt\client.py", line 116, in
import dns.resolver
File "C:\Python\311_x64\Lib\site-packages\dns\resolver.py", line 30, in
import dns._ddr
File "C:\Python\311_x64\Lib\site-packages\dns\_ddr.py", line 12, in
import dns.nameserver
File "C:\Python\311_x64\Lib\site-packages\dns\nameserver.py", line 5, in
import dns.asyncquery
File "C:\Python\311_x64\Lib\site-packages\dns\asyncquery.py", line 38, in
from dns.query import (
File "C:\Python\311_x64\Lib\site-packages\dns\query.py", line 64, in
import httpcore._backends.sync
File "C:\Python\311_x64\Lib\site-packages\httpcore\__init__.py", line 1, in
from ._api import request, stream
File "C:\Python\311_x64\Lib\site-packages\httpcore\_api.py", line 5, in
from ._sync.connection_pool import ConnectionPool
File "C:\Python\311_x64\Lib\site-packages\httpcore\_sync\__init__.py", line 1, in
from .connection import HTTPConnection
File "C:\Python\311_x64\Lib\site-packages\httpcore\_sync\connection.py", line 12, in
from .._synchronization import Lock
File "C:\Python\311_x64\Lib\site-packages\httpcore\_synchronization.py", line 16, in
import anyio
File "C:\Python\311_x64\Lib\site-packages\anyio\__init__.py", line 21, in
from ._core._fileio import AsyncFile as AsyncFile
File "C:\Python\311_x64\Lib\site-packages\anyio\_core\_fileio.py", line 21, in
from .. import to_thread
File "C:\Python\311_x64\Lib\site-packages\anyio\to_thread.py", line 9, in
from .abc import CapacityLimiter
File "C:\Python\311_x64\Lib\site-packages\anyio\abc\__init__.py", line 50, in
from ..from_thread import BlockingPortal as BlockingPortal
File "C:\Python\311_x64\Lib\site-packages\anyio\from_thread.py", line 6, in
from concurrent.futures import FIRST_COMPLETED, Future, ThreadPoolExecutor, wait
File "", line 1229, in _handle_fromlist
File "C:\Python\311_x64\Lib\concurrent\futures\__init__.py", line 49, in __getattr__
from .thread import ThreadPoolExecutor as te
File "C:\Python\311_x64\Lib\concurrent\futures\thread.py", line 37, in
threading._register_atexit(_python_exit)
File "C:\Python\311_x64\Lib\threading.py", line 1527, in _register_atexit
raise RuntimeError("can't register atexit after shutdown")
RuntimeError: can't register atexit after shutdown
Contributor guide
Assessment
This issue has not been assessed yet.