Commit b811752
authored
Fix race condition (#946)
Naveed will recognize this: we were trying to help Bijan, and this was a red herring along the way.
The problem sequence of events is this:
- We create a new `_ModuleHandler` (line 67), which constructs the `_SingletonEventLoopThread` (line 73).
- Creating that singleton sets `self._loop` to None (old line 36), then spawns a separate thread (line 39) which would properly initialize `self._loop` (old line 42).
- Before the separate thread can run, though, we call `module_handler.emit()` (line 85), which calls `self._worker.get_loop()` (line 94), which throws an exception because the loop isn't initialized yet, because the second thread hasn't had a chance to run yet.
With the changes in the first commit, the loop is initialized immediately and merely run in the other thread. That way, other things (like `emit()`) can enqueue new coroutines in the loop before it starts running, and they'll execute shortly thereafter when the other thread gets going.
While I was at this, I noticed that we were using [a non-thread-safe](https://docs.python.org/3/library/asyncio-sync.html#event) `asyncio.Event` to signal between multiple threads, so I changed it to use a `threading.Event` instead. That's in a separate commit, in case that turns out to be a bad idea (I still don't have enough experience with asyncio to feel confident about that stuff).1 parent e41f0d4 commit b811752
1 file changed
+9
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
27 | | - | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
36 | | - | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
40 | 42 | | |
41 | 43 | | |
42 | | - | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
57 | | - | |
58 | | - | |
| 58 | + | |
| 59 | + | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
| |||
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
104 | | - | |
| 105 | + | |
105 | 106 | | |
106 | 107 | | |
107 | 108 | | |
| |||
0 commit comments