-
-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
trio.TrioInternalError when sync func run with trio.to_thread.run_sync returns #2878
Comments
Python 3.11.5 |
I'm not certain, but I suspect your issue might be with the |
It didn't matter, got the same error. Here's additional exceptions that I forgot to post earlier:
Hope it helps. |
I can reproduce your failure with the original code you posted, but I don't see it anymore after the fix that @TeamSpen210 suggested. Specifically, I changed |
I can confirm that fix suggested by @TeamSpen210 works on Ubuntu 22.04 running Python 3.10.12 and save version of trio, but not on Windows 11 where it was originally seen. |
One other thing you could do is simplify the code a bit by getting rid of |
I personally cannot reproduce after applying @TeamSpen210's fix on Windows 11, running trio as of dfa5576, and CPython 3.11.4. At least I assume it's supposed to hang after this output?:
If so! Then maybe still breaking after that fix is a more machine specific thing? As for the original issue, I haven't looked at the source code at all but maybe there's some cheap way to check for concurrency and error in most cases? (without a lock, obviously...) |
Incidentally the thread-unsafe operation you did was checking for a termination message, which is no longer really required in trio>=0.23 if you are using import trio
import time
start = 0
class dummy_driver:
def get_handler(self, msg):
if msg == 'step':
return self.run_step
def run_step(self, ext_comms, send_resp):
time.sleep(2)
# trio.sleep(2)
print(f"handler(): sending response, time = {int(time.time())-start}")
send_resp(ext_comms, 'ok')
class dummy_io:
def get(self):
time.sleep(2)
return 'step'
def put(self, resp):
time.sleep(1)
print(f"received resp {resp}")
# reads msg from the comms channel from TEAL and sends it back to main driver thread
def read_msg(ext_comms, msg_in: trio.MemoryReceiveChannel, msg_out: trio.MemorySendChannel):
try:
while True:
msg = ext_comms.get() # blocks
# This line will notice cancellation and end the thread
trio.from_thread.run(msg_out.send, msg)
finally:
print(f"read_msg: terminating, time = {int(time.time())-start}")
def send_resp(ext_comms, resp):
if resp:
ext_comms.put(resp)
async def async_main():
driver = dummy_driver()
ext_comms = dummy_io()
send, receive = trio.open_memory_channel(0)
send2, receive2 = trio.open_memory_channel(0)
async with trio.open_nursery() as nursery:
count = 0
nursery.start_soon(trio.to_thread.run_sync, read_msg, ext_comms, receive2, send)
while True:
msg = await receive.receive()
fn = driver.get_handler(msg)
print(f"main: assign item to handler, time = {int(time.time())-start}")
nursery.start_soon(trio.to_thread.run_sync, fn, ext_comms, send_resp)
count += 1
if count == 5:
print(f"main: terminating, time = {int(time.time())-start}")
# just cancel everything to exit
nursery.cancel_scope.cancel()
if __name__ == "__main__":
start = int(time.time())
trio.run(async_main)
print(f"main: terminated, time = {int(time.time())-start}") |
In my tests it did that unless I added a
I think it already would fail when |
Thanks @TeamSpen210. That solves the problem for me. Please feel free to close or let me know if you want me to close the issue if you don't need it to resolve the trio.TrioInternalError.
|
Version: 0.23.1
Just trying out trio with some test code and got this:
Here's the test code that caused it:
This happened when count reached 5 and read_msg() returned.
The text was updated successfully, but these errors were encountered: