tomer8007 / kik-bot-api-unofficial

Python API for writing unoffical Kik bots that act like humans
MIT License
126 stars 76 forks source link

"can't register atexit after shutdown" in Python 3.9 #172

Closed PascalVH closed 2 years ago

PascalVH commented 3 years ago

If I want to start one of the examples (echo_bot.py or interactive_client.py) I get an exception ... register_client.py worked like a charm and the cretaed account works on a BlueStacks device.

The exception: [2020-10-26 20:47:45,249] DEBUG (thread MainThread): Using proactor: IocpProactor [2020-10-26 20:47:45,249] INFO (thread Kik Connection): [+] Initiating the Kik Connection thread and connecting to kik server... Exception in thread Kik Connection: Traceback (most recent call last): File "C:\Program Files\Python39\lib\threading.py", line 950, in _bootstrap_inner self.run() File "C:\Program Files\Python39\lib\threading.py", line 888, in run self._target(*self._args, **self._kwargs) File "E:\KikBotUnofficial\kik_unofficial\client.py", line 639, in _kik_connection_thread_function self.loop.run_until_complete(coro) File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1017, in create_connection infos = await self._ensure_resolved( File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1396, in _ensure_resolved return await loop.getaddrinfo(host, port, family=family, type=type, File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 856, in getaddrinfo return await self.run_in_executor( File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 809, in run_in_executor executor = concurrent.futures.ThreadPoolExecutor( File "C:\Program Files\Python39\lib\concurrent\futures__init.py", line 49, in getattr__ from .thread import ThreadPoolExecutor as te File "C:\Program Files\Python39\lib\concurrent\futures\thread.py", line 37, in threading._register_atexit(_python_exit) File "C:\Program Files\Python39\lib\threading.py", line 1370, in _register_atexit raise RuntimeError("can't register atexit after shutdown") RuntimeError: can't register atexit after shutdown

Any idea? I Know I'm running on a Windows machine what is not really standard but other Python applications (e.g. The official Kik) does work. Any help would be highly appreciated!

tomer8007 commented 3 years ago

Well, does this work on regular (real) machines?

PascalVH commented 3 years ago

Well, does this work on regular (real) machines?

I Don't know what you mean by that ... In the meanwhile I tested it on a CentOS 8.2 machine and there I've got it to work. On the Windows machine however I'm not able to make it work. ALthough I can continue now on the Linux machine, I'm still wondering why it doesn't work on the Windows machine ... What (dependency) am I missing?

The install works without any errors:

PS E:> pip3 install .\kik-bot-api-unofficial-new\ Processing e:\kik-bot-api-unofficial-new Requirement already satisfied: pbkdf2 in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (1.3) Requirement already satisfied: rsa in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (4.6) Requirement already satisfied: lxml in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (4.6.1) Requirement already satisfied: bs4 in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (0.0.1) Requirement already satisfied: protobuf in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (3.13.0) Requirement already satisfied: requests in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (2.24.0) Requirement already satisfied: pillow in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (8.0.1) Requirement already satisfied: pyDes in c:\program files\python39\lib\site-packages (from kik-unofficial==0.3) (2.0.1) Requirement already satisfied: pyasn1>=0.1.3 in c:\program files\python39\lib\site-packages (from rsa->kik-unofficial==0.3) (0.4.8) Requirement already satisfied: beautifulsoup4 in c:\program files\python39\lib\site-packages (from bs4->kik-unofficial==0.3) (4.9.3) Requirement already satisfied: setuptools in c:\program files\python39\lib\site-packages (from protobuf->kik-unofficial==0.3) (49.2.1) Requirement already satisfied: six>=1.9 in c:\program files\python39\lib\site-packages (from protobuf->kik-unofficial==0.3) (1.15.0) Requirement already satisfied: certifi>=2017.4.17 in c:\program files\python39\lib\site-packages (from requests->kik-unofficial==0.3) (2020.6.20) Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\program files\python39\lib\site-packages (from requests->kik-unofficial==0.3) (1.25.11) Requirement already satisfied: idna<3,>=2.5 in c:\program files\python39\lib\site-packages (from requests->kik-unofficial==0.3) (2.10) Requirement already satisfied: chardet<4,>=3.0.2 in c:\program files\python39\lib\site-packages (from requests->kik-unofficial==0.3) (3.0.4) Requirement already satisfied: soupsieve>1.2; python_version >= "3.0" in c:\program files\python39\lib\site-packages (from beautifulsoup4->bs4->kik-unofficial==0.3) (2.0.1) Using legacy 'setup.py install' for kik-unofficial, since package 'wheel' is not installed. Installing collected packages: kik-unofficial Attempting uninstall: kik-unofficial Found existing installation: kik-unofficial 0.3 Uninstalling kik-unofficial-0.3: Successfully uninstalled kik-unofficial-0.3 Running setup.py install for kik-unofficial ... done Successfully installed kik-unofficial-0.3

But when I try to run one of the examples:

E:>python.exe E:\kik-bot-api-unofficial-new\examples\echo_bot.py [USERNAME] [PASSWORD] [2020-10-27 08:46:31,379] INFO (thread Kik Connection): [+] Initiating the Kik Connection thread and connecting to kik server... Exception in thread Kik Connection: Traceback (most recent call last): File "C:\Program Files\Python39\lib\threading.py", line 950, in _bootstrap_inner self.run() File "C:\Program Files\Python39\lib\threading.py", line 888, in run self._target(*self._args, **self._kwargs) File "C:\Program Files\Python39\lib\site-packages\kik_unofficial\client.py", line 639, in _kik_connection_thread_function self.loop.run_until_complete(coro) File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete return future.result() File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1017, in create_connection infos = await self._ensure_resolved( File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 1396, in _ensure_resolved return await loop.getaddrinfo(host, port, family=family, type=type, File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 856, in getaddrinfo return await self.run_in_executor( File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 809, in run_in_executor executor = concurrent.futures.ThreadPoolExecutor( File "C:\Program Files\Python39\lib\concurrent\futures__init.py", line 49, in getattr__ from .thread import ThreadPoolExecutor as te File "C:\Program Files\Python39\lib\concurrent\futures\thread.py", line 37, in threading._register_atexit(_python_exit) File "C:\Program Files\Python39\lib\threading.py", line 1370, in _register_atexit raise RuntimeError("can't register atexit after shutdown") RuntimeError: can't register atexit after shutdown

bluemods commented 3 years ago

I'm having the same issue on a Windows 10 machine. Please fix this.

PascalVH commented 3 years ago

I'm having the issue on both Windows Server 2019 and Windows 10. I Can't get it to work anymore on Windows and can't find why. 🤷🏻‍♂️

tomer8007 commented 3 years ago

echo_bot.py seems to work fine for me on Windows with Python 3.7 and PyCharm. Will try other combinations.

PascalVH commented 3 years ago

I Did some additional tests. All on Windows Server 2019 (Version 10.0.17763.1577) but results will be similar on Windows 10, I presume. Python v3.7.9 => Ok. Python v3.8.6 => Ok. Python v3.9.0 => NOk.

On my Linux system, I also use Python v3.8.6 I Do not have access to a Linux system that I can upgrade to Python v3.9.0.

However, my preliminary conclusion is that the problem probably is with the use of Python v3.9.0 and not with Windows 10 (Or 2019) ...

tomer8007 commented 3 years ago

OK, after some debugging it appears that this commit in Python 3.9 is causing the problem. Also here's a minimal test case that demonstrates it.

import asyncio
from asyncio import Protocol
import threading

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

def another_thread():
    time.sleep(1)
    coro = loop.create_connection(lambda: TestProtocol(), "google.com", 80)
    loop.run_until_complete(coro)

test_thread = threading.Thread(target=another_thread)
test_thread.start()

class TestProtocol(Protocol):
    def __init__(self):
        pass

    def connection_made(self, transport):
        print("connection success")

This code prints connection success on Python < 3.9, but prints an exception on Python 3.9. Apparently because it thinks everything is shutting down so it doesn't let you run coroutines.

tomer8007 commented 3 years ago

Also a temporary bypass is to add while True: pass in the end of the script.

TheGreatCodeholio commented 2 years ago

When you start the thread with thread.start() the main process exits. Since we want the child thread to run forever even if the main process exits we have to call thread.join() after thread.start()

By doing so it now works in python > 3.8

Also needed to move the class to above where the thread is called otherwise class functions isn't found.

import asyncio
import time
from asyncio import Protocol
import threading

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

class TestProtocol(Protocol):
    def __init__(self):
        pass

    def connection_made(self, transport):
        print("connection success")

def another_thread():
    time.sleep(1)
    coro = loop.create_connection(lambda: TestProtocol(), "google.com", 80)
    loop.run_until_complete(coro)

test_thread = threading.Thread(target=another_thread)
test_thread.start()
test_thread.join()
ilike3pancakes commented 11 months ago

I still have this issue on latest commit (on new)

[2023-07-29 23:42:53,477] INFO   (thread Thread-5 (_on_new_data_received)): [+] Closing current connection and creating a new authenticated one.
[2023-07-29 23:42:53,477] INFO   (thread Thread-5 (_on_new_data_received)): [!] Disconnecting.
[2023-07-29 23:42:54,479] INFO   (thread Kik Connection): [+] Initiating the Kik Connection thread and connecting to kik server...
Exception in thread Kik Connection:
Traceback (most recent call last):
  File "/home/b/miniconda3/envs/lama/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/home/b/miniconda3/envs/lama/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/home/b/miniconda3/envs/lama/lib/python3.10/site-packages/kik_unofficial/client.py", line 677, in _kik_connection_thread_function
    self.loop.run_until_complete(connection_coroutine)
  File "/home/b/miniconda3/envs/lama/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/b/miniconda3/envs/lama/lib/python3.10/asyncio/base_events.py", line 1036, in create_connection
    infos = await self._ensure_resolved(
  File "/home/b/miniconda3/envs/lama/lib/python3.10/asyncio/base_events.py", line 1418, in _ensure_resolved
    return await loop.getaddrinfo(host, port, family=family, type=type,
  File "/home/b/miniconda3/envs/lama/lib/python3.10/asyncio/base_events.py", line 863, in getaddrinfo
    return await self.run_in_executor(
  File "/home/b/miniconda3/envs/lama/lib/python3.10/asyncio/base_events.py", line 821, in run_in_executor
    executor.submit(func, *args), loop=self)
  File "/home/b/miniconda3/envs/lama/lib/python3.10/concurrent/futures/thread.py", line 167, in submit
    raise RuntimeError('cannot schedule new futures after shutdown')
RuntimeError: cannot schedule new futures after shutdown
(lama) b@LAPTOP-TSH380PC:~/workspace/kik-bot-api-unofficial$ cat ^C
(lama) b@LAPTOP-TSH380PC:~/workspace/kik-bot-api-unofficial$ ls
Dockerfile  LICENSE  README.md  build  creds.yaml  docker-compose.yml  examples  kik_unofficial  kik_unofficial.egg-info  setup.cfg  setup.py
(lama) b@LAPTOP-TSH380PC:~/workspace/kik-bot-api-unofficial$ pip freeze | grep kik
kik-unofficial @ file:///home/b/workspace/kik-bot-api-unofficial
(lama) b@LAPTOP-TSH380PC:~/workspace/kik-bot-api-unofficial$ git log | grep  7547162
commit 75471620fdbb93061a11c1b31366479509b25022
(lama) b@LAPTOP-TSH380PC:~/workspace/kik-bot-api-unofficial$ python --version
Python 3.10.12
ilike3pancakes commented 11 months ago

@tomer8007 I confirm the issue is absent on python 3.8

this is WSL2 running on windows 11. Python installed in conda.