Signal Handling Code colliding?


I am new to Trio (and to async) and am trying to incorporate this library into my trading program, which uses Alpaca Trading API. When I am running this small test:

import trio
import trade_class as TC
from datetime import datetime
from os import path

async def my_sleep():
    sym = 'AAPL'
    barset = tc.api.get_barset(sym, '1Min', limit=50 * 6.5 * 7,[sym]
    return barset

async def child(i):
    print(f"  child{i}: started! sleeping now...")
    await trio.sleep(5)
    print(f"  child{i}: exiting!")

async def parent():
    print("parent: started!")
    async with trio.open_nursery() as nursery:
        for i in range(1, 3):
            print(f"parent: spawning child{i}...")
            nursery.start_soon(child, i)
        print("parent: waiting for children to finish...")
        # -- we exit the nursery block here --
    print("parent: all done!")

FULL_DATA_FOLDER = path.join(path.dirname(path.abspath(__file__)), 'data')
key_filename = path.join(FULL_DATA_FOLDER, 'key1.txt')
tc = TC.Alpaca_Obj(key_filename)

– I get the following error:

c:\users\gil\envs\utils\lib\site-packages\trio\_core\ RuntimeWarning: It looks like Trio's signal handling code might have collided with another library you're using. If you're running Trio in guest mode, then this might mean you should set host_uses_signal_set_wakeup_fd=True. Otherwise, file a bug on Trio and we'll help you figure out what's going on.

I think the Alpaca API uses async/websockets, but I don’t invoke these in my own code.

With thanks!

That warning means that someone is overriding the Python interpreter’s normal handling for control-C / KeyboardInterrupt, before you import trio. Note that it’s only a warning, not an actual error – control-C handling might be wonky afterwards, but your program will still continue.

So I guess the question is who’s doing that and why :-). I would guess it was the Alpaca Trading API, but you’re importing that after Trio, so that doesn’t quite make sense…

Do you have any custom Python configuration that might be messing with control-C handling? How are you executing your script – in a terminal, in an IDE, …?


I am running this code with ipython from inside a neovim terminal, so that might be it.

Also, I tried do comment out the Alpaca import to see if the warning is gone – and it was gone, but then when I removed the comment to reuse again the Alpaca import – the warning did not appear again!

In sum, the following is weird:

  1. Neovim / IPython issues?
  2. As you note, the Alpaca import occurs after the trio import, but still effects the warning.
  3. there is some states issue here, because the warning does not return when I re-run with the Alpaca import back (to clarify: only if I exit ipython and re-enter, the warning appears).

Also: Alpaca is not written in async, so I am hitting a brick wall anyway with this. Is there an easy recipe for making the Alpaca (or any other REST API module) async (they are using requests)?

I know the above is basic, I am just staring this async trio journey!

Thanks again,

Quick update:

The same error occurs when I run the code from the command line using python (and not ipython), so we can check off anything related to IDE in relation to this.

I think I found it. The culprit was an import from ipdb:

from ipdb import set_trace

Is this a familiar issue with trio?


Huh, interesting!

No, this is the first we’ve heard of it. Lucky you :-).

What does signal.getsignal(signal.SIGINT) say, just before you do import trio?

I think this turned off the warning.