-
-
Notifications
You must be signed in to change notification settings - Fork 322
/
test_threads.py
1017 lines (806 loc) · 33.2 KB
/
test_threads.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import contextvars
import queue as stdlib_queue
import re
import sys
import threading
import time
import weakref
from functools import partial
from typing import Callable, Optional
import pytest
import sniffio
from .. import CapacityLimiter, Event, _core, fail_after, sleep, sleep_forever
from .._core._tests.test_ki import ki_self
from .._core._tests.tutil import buggy_pypy_asyncgens
from .._threads import (
current_default_thread_limiter,
from_thread_check_cancelled,
from_thread_run,
from_thread_run_sync,
to_thread_run_sync,
)
from ..testing import wait_all_tasks_blocked
async def test_do_in_trio_thread():
trio_thread = threading.current_thread()
async def check_case(do_in_trio_thread, fn, expected, trio_token=None):
record = []
def threadfn():
try:
record.append(("start", threading.current_thread()))
x = do_in_trio_thread(fn, record, trio_token=trio_token)
record.append(("got", x))
except BaseException as exc:
print(exc)
record.append(("error", type(exc)))
child_thread = threading.Thread(target=threadfn, daemon=True)
child_thread.start()
while child_thread.is_alive():
print("yawn")
await sleep(0.01)
assert record == [("start", child_thread), ("f", trio_thread), expected]
token = _core.current_trio_token()
def f(record):
assert not _core.currently_ki_protected()
record.append(("f", threading.current_thread()))
return 2
await check_case(from_thread_run_sync, f, ("got", 2), trio_token=token)
def f(record):
assert not _core.currently_ki_protected()
record.append(("f", threading.current_thread()))
raise ValueError
await check_case(from_thread_run_sync, f, ("error", ValueError), trio_token=token)
async def f(record):
assert not _core.currently_ki_protected()
await _core.checkpoint()
record.append(("f", threading.current_thread()))
return 3
await check_case(from_thread_run, f, ("got", 3), trio_token=token)
async def f(record):
assert not _core.currently_ki_protected()
await _core.checkpoint()
record.append(("f", threading.current_thread()))
raise KeyError
await check_case(from_thread_run, f, ("error", KeyError), trio_token=token)
async def test_do_in_trio_thread_from_trio_thread():
with pytest.raises(RuntimeError):
from_thread_run_sync(lambda: None) # pragma: no branch
async def foo(): # pragma: no cover
pass
with pytest.raises(RuntimeError):
from_thread_run(foo)
def test_run_in_trio_thread_ki():
# if we get a control-C during a run_in_trio_thread, then it propagates
# back to the caller (slick!)
record = set()
async def check_run_in_trio_thread():
token = _core.current_trio_token()
def trio_thread_fn():
print("in Trio thread")
assert not _core.currently_ki_protected()
print("ki_self")
try:
ki_self()
finally:
import sys
print("finally", sys.exc_info())
async def trio_thread_afn():
trio_thread_fn()
def external_thread_fn():
try:
print("running")
from_thread_run_sync(trio_thread_fn, trio_token=token)
except KeyboardInterrupt:
print("ok1")
record.add("ok1")
try:
from_thread_run(trio_thread_afn, trio_token=token)
except KeyboardInterrupt:
print("ok2")
record.add("ok2")
thread = threading.Thread(target=external_thread_fn)
thread.start()
print("waiting")
while thread.is_alive():
await sleep(0.01)
print("waited, joining")
thread.join()
print("done")
_core.run(check_run_in_trio_thread)
assert record == {"ok1", "ok2"}
def test_await_in_trio_thread_while_main_exits():
record = []
ev = Event()
async def trio_fn():
record.append("sleeping")
ev.set()
await _core.wait_task_rescheduled(lambda _: _core.Abort.SUCCEEDED)
def thread_fn(token):
try:
from_thread_run(trio_fn, trio_token=token)
except _core.Cancelled:
record.append("cancelled")
async def main():
token = _core.current_trio_token()
thread = threading.Thread(target=thread_fn, args=(token,))
thread.start()
await ev.wait()
assert record == ["sleeping"]
return thread
thread = _core.run(main)
thread.join()
assert record == ["sleeping", "cancelled"]
async def test_named_thread():
ending = " from trio._tests.test_threads.test_named_thread"
def inner(name: str = "inner" + ending) -> threading.Thread:
assert threading.current_thread().name == name
return threading.current_thread()
def f(name: str) -> Callable[[None], threading.Thread]:
return partial(inner, name)
# test defaults
await to_thread_run_sync(inner)
await to_thread_run_sync(inner, thread_name=None)
# functools.partial doesn't have __name__, so defaults to None
await to_thread_run_sync(f("None" + ending))
# test that you can set a custom name, and that it's reset afterwards
async def test_thread_name(name: str) -> None:
thread = await to_thread_run_sync(f(name), thread_name=name)
assert re.match("Trio thread [0-9]*", thread.name)
await test_thread_name("")
await test_thread_name("fobiedoo")
await test_thread_name("name_longer_than_15_characters")
await test_thread_name("💙")
def _get_thread_name(ident: Optional[int] = None) -> Optional[str]:
import ctypes
import ctypes.util
libpthread_path = ctypes.util.find_library("pthread")
if not libpthread_path:
print(f"no pthread on {sys.platform})")
return None
libpthread = ctypes.CDLL(libpthread_path)
pthread_getname_np = getattr(libpthread, "pthread_getname_np", None)
# this should never fail on any platforms afaik
assert pthread_getname_np
# thankfully getname signature doesn't differ between platforms
pthread_getname_np.argtypes = [
ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_size_t,
]
pthread_getname_np.restype = ctypes.c_int
name_buffer = ctypes.create_string_buffer(b"", size=16)
if ident is None:
ident = threading.get_ident()
assert pthread_getname_np(ident, name_buffer, 16) == 0
try:
return name_buffer.value.decode()
except UnicodeDecodeError as e: # pragma: no cover
# used for debugging when testing via CI
pytest.fail(f"value: {name_buffer.value!r}, exception: {e}")
# test os thread naming
# this depends on pthread being available, which is the case on 99.9% of linux machines
# and most mac machines. So unless the platform is linux it will just skip
# in case it fails to fetch the os thread name.
async def test_named_thread_os():
def inner(name: str) -> threading.Thread:
os_thread_name = _get_thread_name()
if os_thread_name is None and sys.platform != "linux":
pytest.skip(f"no pthread OS support on {sys.platform}")
else:
assert os_thread_name == name[:15]
return threading.current_thread()
def f(name: str) -> Callable[[None], threading.Thread]:
return partial(inner, name)
# test defaults
default = "None from trio._tests.test_threads.test_named_thread"
await to_thread_run_sync(f(default))
await to_thread_run_sync(f(default), thread_name=None)
# test that you can set a custom name, and that it's reset afterwards
async def test_thread_name(name: str, expected: Optional[str] = None) -> None:
if expected is None:
expected = name
thread = await to_thread_run_sync(f(expected), thread_name=name)
os_thread_name = _get_thread_name(thread.ident)
assert os_thread_name is not None, "should skip earlier if this is the case"
assert re.match("Trio thread [0-9]*", os_thread_name)
await test_thread_name("")
await test_thread_name("fobiedoo")
await test_thread_name("name_longer_than_15_characters")
await test_thread_name("💙", expected="?")
async def test_has_pthread_setname_np():
from trio._core._thread_cache import get_os_thread_name_func
k = get_os_thread_name_func()
if k is None:
assert sys.platform != "linux"
pytest.skip(f"no pthread_setname_np on {sys.platform}")
async def test_run_in_worker_thread():
trio_thread = threading.current_thread()
def f(x):
return (x, threading.current_thread())
x, child_thread = await to_thread_run_sync(f, 1)
assert x == 1
assert child_thread != trio_thread
def g():
raise ValueError(threading.current_thread())
with pytest.raises(ValueError) as excinfo:
await to_thread_run_sync(g)
print(excinfo.value.args)
assert excinfo.value.args[0] != trio_thread
async def test_run_in_worker_thread_cancellation():
register = [None]
def f(q):
# Make the thread block for a controlled amount of time
register[0] = "blocking"
q.get()
register[0] = "finished"
async def child(q, cancellable):
record.append("start")
try:
return await to_thread_run_sync(f, q, cancellable=cancellable)
finally:
record.append("exit")
record = []
q = stdlib_queue.Queue()
async with _core.open_nursery() as nursery:
nursery.start_soon(child, q, True)
# Give it a chance to get started. (This is important because
# to_thread_run_sync does a checkpoint_if_cancelled before
# blocking on the thread, and we don't want to trigger this.)
await wait_all_tasks_blocked()
assert record == ["start"]
# Then cancel it.
nursery.cancel_scope.cancel()
# The task exited, but the thread didn't:
assert register[0] != "finished"
# Put the thread out of its misery:
q.put(None)
while register[0] != "finished":
time.sleep(0.01)
# This one can't be cancelled
record = []
register[0] = None
async with _core.open_nursery() as nursery:
nursery.start_soon(child, q, False)
await wait_all_tasks_blocked()
nursery.cancel_scope.cancel()
with _core.CancelScope(shield=True):
for _ in range(10):
await _core.checkpoint()
# It's still running
assert record == ["start"]
q.put(None)
# Now it exits
# But if we cancel *before* it enters, the entry is itself a cancellation
# point
with _core.CancelScope() as scope:
scope.cancel()
await child(q, False)
assert scope.cancelled_caught
# Make sure that if trio.run exits, and then the thread finishes, then that's
# handled gracefully. (Requires that the thread result machinery be prepared
# for call_soon to raise RunFinishedError.)
def test_run_in_worker_thread_abandoned(capfd, monkeypatch):
monkeypatch.setattr(_core._thread_cache, "IDLE_TIMEOUT", 0.01)
q1 = stdlib_queue.Queue()
q2 = stdlib_queue.Queue()
def thread_fn():
q1.get()
q2.put(threading.current_thread())
async def main():
async def child():
await to_thread_run_sync(thread_fn, cancellable=True)
async with _core.open_nursery() as nursery:
nursery.start_soon(child)
await wait_all_tasks_blocked()
nursery.cancel_scope.cancel()
_core.run(main)
q1.put(None)
# This makes sure:
# - the thread actually ran
# - that thread has finished before we check for its output
thread = q2.get()
while thread.is_alive():
time.sleep(0.01) # pragma: no cover
# Make sure we don't have a "Exception in thread ..." dump to the console:
out, err = capfd.readouterr()
assert "Exception in thread" not in out
assert "Exception in thread" not in err
@pytest.mark.parametrize("MAX", [3, 5, 10])
@pytest.mark.parametrize("cancel", [False, True])
@pytest.mark.parametrize("use_default_limiter", [False, True])
async def test_run_in_worker_thread_limiter(MAX, cancel, use_default_limiter):
# This test is a bit tricky. The goal is to make sure that if we set
# limiter=CapacityLimiter(MAX), then in fact only MAX threads are ever
# running at a time, even if there are more concurrent calls to
# to_thread_run_sync, and even if some of those are cancelled. And
# also to make sure that the default limiter actually limits.
COUNT = 2 * MAX
gate = threading.Event()
lock = threading.Lock()
if use_default_limiter:
c = current_default_thread_limiter()
orig_total_tokens = c.total_tokens
c.total_tokens = MAX
limiter_arg = None
else:
c = CapacityLimiter(MAX)
orig_total_tokens = MAX
limiter_arg = c
try:
# We used to use regular variables and 'nonlocal' here, but it turns
# out that it's not safe to assign to closed-over variables that are
# visible in multiple threads, at least as of CPython 3.10 and PyPy
# 7.3:
#
# https://bugs.python.org/issue30744
# https://bitbucket.org/pypy/pypy/issues/2591/
#
# Mutating them in-place is OK though (as long as you use proper
# locking etc.).
class state:
pass
state.ran = 0
state.high_water = 0
state.running = 0
state.parked = 0
token = _core.current_trio_token()
def thread_fn(cancel_scope):
print("thread_fn start")
from_thread_run_sync(cancel_scope.cancel, trio_token=token)
with lock:
state.ran += 1
state.running += 1
state.high_water = max(state.high_water, state.running)
# The Trio thread below watches this value and uses it as a
# signal that all the stats calculations have finished.
state.parked += 1
gate.wait()
with lock:
state.parked -= 1
state.running -= 1
print("thread_fn exiting")
async def run_thread(event):
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(
thread_fn, cancel_scope, limiter=limiter_arg, cancellable=cancel
)
print("run_thread finished, cancelled:", cancel_scope.cancelled_caught)
event.set()
async with _core.open_nursery() as nursery:
print("spawning")
events = []
for i in range(COUNT):
events.append(Event())
nursery.start_soon(run_thread, events[-1])
await wait_all_tasks_blocked()
# In the cancel case, we in particular want to make sure that the
# cancelled tasks don't release the semaphore. So let's wait until
# at least one of them has exited, and that everything has had a
# chance to settle down from this, before we check that everyone
# who's supposed to be waiting is waiting:
if cancel:
print("waiting for first cancellation to clear")
await events[0].wait()
await wait_all_tasks_blocked()
# Then wait until the first MAX threads are parked in gate.wait(),
# and the next MAX threads are parked on the semaphore, to make
# sure no-one is sneaking past, and to make sure the high_water
# check below won't fail due to scheduling issues. (It could still
# fail if too many threads are let through here.)
while state.parked != MAX or c.statistics().tasks_waiting != MAX:
await sleep(0.01) # pragma: no cover
# Then release the threads
gate.set()
assert state.high_water == MAX
if cancel:
# Some threads might still be running; need to wait to them to
# finish before checking that all threads ran. We can do this
# using the CapacityLimiter.
while c.borrowed_tokens > 0:
await sleep(0.01) # pragma: no cover
assert state.ran == COUNT
assert state.running == 0
finally:
c.total_tokens = orig_total_tokens
async def test_run_in_worker_thread_custom_limiter():
# Basically just checking that we only call acquire_on_behalf_of and
# release_on_behalf_of, since that's part of our documented API.
record = []
class CustomLimiter:
async def acquire_on_behalf_of(self, borrower):
record.append("acquire")
self._borrower = borrower
def release_on_behalf_of(self, borrower):
record.append("release")
assert borrower == self._borrower
await to_thread_run_sync(lambda: None, limiter=CustomLimiter())
assert record == ["acquire", "release"]
async def test_run_in_worker_thread_limiter_error():
record = []
class BadCapacityLimiter:
async def acquire_on_behalf_of(self, borrower):
record.append("acquire")
def release_on_behalf_of(self, borrower):
record.append("release")
raise ValueError
bs = BadCapacityLimiter()
with pytest.raises(ValueError) as excinfo:
await to_thread_run_sync(lambda: None, limiter=bs)
assert excinfo.value.__context__ is None
assert record == ["acquire", "release"]
record = []
# If the original function raised an error, then the semaphore error
# chains with it
d = {}
with pytest.raises(ValueError) as excinfo:
await to_thread_run_sync(lambda: d["x"], limiter=bs)
assert isinstance(excinfo.value.__context__, KeyError)
assert record == ["acquire", "release"]
async def test_run_in_worker_thread_fail_to_spawn(monkeypatch):
# Test the unlikely but possible case where trying to spawn a thread fails
def bad_start(self, *args):
raise RuntimeError("the engines canna take it captain")
monkeypatch.setattr(_core._thread_cache.ThreadCache, "start_thread_soon", bad_start)
limiter = current_default_thread_limiter()
assert limiter.borrowed_tokens == 0
# We get an appropriate error, and the limiter is cleanly released
with pytest.raises(RuntimeError) as excinfo:
await to_thread_run_sync(lambda: None) # pragma: no cover
assert "engines" in str(excinfo.value)
assert limiter.borrowed_tokens == 0
async def test_trio_to_thread_run_sync_token():
# Test that to_thread_run_sync automatically injects the current trio token
# into a spawned thread
def thread_fn():
callee_token = from_thread_run_sync(_core.current_trio_token)
return callee_token
caller_token = _core.current_trio_token()
callee_token = await to_thread_run_sync(thread_fn)
assert callee_token == caller_token
async def test_trio_to_thread_run_sync_expected_error():
# Test correct error when passed async function
async def async_fn(): # pragma: no cover
pass
with pytest.raises(TypeError, match="expected a sync function"):
await to_thread_run_sync(async_fn)
trio_test_contextvar: contextvars.ContextVar[str] = contextvars.ContextVar(
"trio_test_contextvar"
)
async def test_trio_to_thread_run_sync_contextvars():
trio_thread = threading.current_thread()
trio_test_contextvar.set("main")
def f():
value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
return (value, threading.current_thread())
value, child_thread = await to_thread_run_sync(f)
assert value == "main"
assert child_thread != trio_thread
def g():
parent_value = trio_test_contextvar.get()
trio_test_contextvar.set("worker")
inner_value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
return (
parent_value,
inner_value,
threading.current_thread(),
)
parent_value, inner_value, child_thread = await to_thread_run_sync(g)
current_value = trio_test_contextvar.get()
assert parent_value == "main"
assert inner_value == "worker"
assert current_value == "main", (
"The contextvar value set on the worker would not propagate back to the main"
" thread"
)
assert sniffio.current_async_library() == "trio"
async def test_trio_from_thread_run_sync():
# Test that to_thread_run_sync correctly "hands off" the trio token to
# trio.from_thread.run_sync()
def thread_fn():
trio_time = from_thread_run_sync(_core.current_time)
return trio_time
trio_time = await to_thread_run_sync(thread_fn)
assert isinstance(trio_time, float)
# Test correct error when passed async function
async def async_fn(): # pragma: no cover
pass
def thread_fn():
from_thread_run_sync(async_fn)
with pytest.raises(TypeError, match="expected a synchronous function"):
await to_thread_run_sync(thread_fn)
async def test_trio_from_thread_run():
# Test that to_thread_run_sync correctly "hands off" the trio token to
# trio.from_thread.run()
record = []
async def back_in_trio_fn():
_core.current_time() # implicitly checks that we're in trio
record.append("back in trio")
def thread_fn():
record.append("in thread")
from_thread_run(back_in_trio_fn)
await to_thread_run_sync(thread_fn)
assert record == ["in thread", "back in trio"]
# Test correct error when passed sync function
def sync_fn(): # pragma: no cover
pass
with pytest.raises(TypeError, match="appears to be synchronous"):
await to_thread_run_sync(from_thread_run, sync_fn)
async def test_trio_from_thread_token():
# Test that to_thread_run_sync and spawned trio.from_thread.run_sync()
# share the same Trio token
def thread_fn():
callee_token = from_thread_run_sync(_core.current_trio_token)
return callee_token
caller_token = _core.current_trio_token()
callee_token = await to_thread_run_sync(thread_fn)
assert callee_token == caller_token
async def test_trio_from_thread_token_kwarg():
# Test that to_thread_run_sync and spawned trio.from_thread.run_sync() can
# use an explicitly defined token
def thread_fn(token):
callee_token = from_thread_run_sync(_core.current_trio_token, trio_token=token)
return callee_token
caller_token = _core.current_trio_token()
callee_token = await to_thread_run_sync(thread_fn, caller_token)
assert callee_token == caller_token
async def test_from_thread_no_token():
# Test that a "raw call" to trio.from_thread.run() fails because no token
# has been provided
with pytest.raises(RuntimeError):
from_thread_run_sync(_core.current_time)
async def test_trio_from_thread_run_sync_contextvars():
trio_test_contextvar.set("main")
def thread_fn():
thread_parent_value = trio_test_contextvar.get()
trio_test_contextvar.set("worker")
thread_current_value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
def back_in_main():
back_parent_value = trio_test_contextvar.get()
trio_test_contextvar.set("back_in_main")
back_current_value = trio_test_contextvar.get()
assert sniffio.current_async_library() == "trio"
return back_parent_value, back_current_value
back_parent_value, back_current_value = from_thread_run_sync(back_in_main)
thread_after_value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
return (
thread_parent_value,
thread_current_value,
thread_after_value,
back_parent_value,
back_current_value,
)
(
thread_parent_value,
thread_current_value,
thread_after_value,
back_parent_value,
back_current_value,
) = await to_thread_run_sync(thread_fn)
current_value = trio_test_contextvar.get()
assert current_value == thread_parent_value == "main"
assert thread_current_value == back_parent_value == thread_after_value == "worker"
assert sniffio.current_async_library() == "trio"
assert back_current_value == "back_in_main"
async def test_trio_from_thread_run_contextvars():
trio_test_contextvar.set("main")
def thread_fn():
thread_parent_value = trio_test_contextvar.get()
trio_test_contextvar.set("worker")
thread_current_value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
async def async_back_in_main():
back_parent_value = trio_test_contextvar.get()
trio_test_contextvar.set("back_in_main")
back_current_value = trio_test_contextvar.get()
assert sniffio.current_async_library() == "trio"
return back_parent_value, back_current_value
back_parent_value, back_current_value = from_thread_run(async_back_in_main)
thread_after_value = trio_test_contextvar.get()
with pytest.raises(sniffio.AsyncLibraryNotFoundError):
sniffio.current_async_library()
return (
thread_parent_value,
thread_current_value,
thread_after_value,
back_parent_value,
back_current_value,
)
(
thread_parent_value,
thread_current_value,
thread_after_value,
back_parent_value,
back_current_value,
) = await to_thread_run_sync(thread_fn)
current_value = trio_test_contextvar.get()
assert current_value == thread_parent_value == "main"
assert thread_current_value == back_parent_value == thread_after_value == "worker"
assert back_current_value == "back_in_main"
assert sniffio.current_async_library() == "trio"
def test_run_fn_as_system_task_catched_badly_typed_token():
with pytest.raises(RuntimeError):
from_thread_run_sync(_core.current_time, trio_token="Not TrioTokentype")
async def test_from_thread_inside_trio_thread():
def not_called(): # pragma: no cover
assert False
trio_token = _core.current_trio_token()
with pytest.raises(RuntimeError):
from_thread_run_sync(not_called, trio_token=trio_token)
@pytest.mark.skipif(buggy_pypy_asyncgens, reason="pypy 7.2.0 is buggy")
def test_from_thread_run_during_shutdown():
save = []
record = []
async def agen(token):
try:
yield
finally:
with _core.CancelScope(shield=True):
try:
await to_thread_run_sync(
partial(from_thread_run, sleep, 0, trio_token=token)
)
except _core.RunFinishedError:
record.append("finished")
else:
record.append("clean")
async def main(use_system_task):
save.append(agen(_core.current_trio_token() if use_system_task else None))
await save[-1].asend(None)
_core.run(main, True) # System nursery will be closed and raise RunFinishedError
_core.run(main, False) # host task will be rescheduled as normal
assert record == ["finished", "clean"]
async def test_trio_token_weak_referenceable():
token = _core.current_trio_token()
assert isinstance(token, _core.TrioToken)
weak_reference = weakref.ref(token)
assert token is weak_reference()
async def test_unsafe_cancellable_kwarg():
# This is a stand in for a numpy ndarray or other objects
# that (maybe surprisingly) lack a notion of truthiness
class BadBool:
def __bool__(self):
raise NotImplementedError
with pytest.raises(NotImplementedError):
await to_thread_run_sync(int, cancellable=BadBool())
async def test_from_thread_reuses_task():
task = _core.current_task()
async def async_current_task():
return _core.current_task()
assert task is await to_thread_run_sync(from_thread_run_sync, _core.current_task)
assert task is await to_thread_run_sync(from_thread_run, async_current_task)
async def test_recursive_to_thread():
tid = None
def get_tid_then_reenter():
nonlocal tid
tid = threading.get_ident()
return from_thread_run(to_thread_run_sync, threading.get_ident)
assert tid != await to_thread_run_sync(get_tid_then_reenter)
async def test_from_thread_host_cancelled():
queue = stdlib_queue.Queue()
def sync_check():
from_thread_run_sync(cancel_scope.cancel)
try:
from_thread_run_sync(bool)
except _core.Cancelled: # pragma: no cover
queue.put(True) # sync functions don't raise Cancelled
else:
queue.put(False)
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(sync_check)
assert not cancel_scope.cancelled_caught
assert not queue.get_nowait()
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(sync_check, cancellable=True)
assert cancel_scope.cancelled_caught
assert not await to_thread_run_sync(partial(queue.get, timeout=1))
async def no_checkpoint():
return True
def async_check():
from_thread_run_sync(cancel_scope.cancel)
try:
assert from_thread_run(no_checkpoint)
except _core.Cancelled: # pragma: no cover
queue.put(True) # async functions raise Cancelled at checkpoints
else:
queue.put(False)
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(async_check)
assert not cancel_scope.cancelled_caught
assert not queue.get_nowait()
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(async_check, cancellable=True)
assert cancel_scope.cancelled_caught
assert not await to_thread_run_sync(partial(queue.get, timeout=1))
async def async_time_bomb():
cancel_scope.cancel()
with fail_after(10):
await sleep_forever()
with _core.CancelScope() as cancel_scope:
await to_thread_run_sync(from_thread_run, async_time_bomb)
assert cancel_scope.cancelled_caught
async def test_from_thread_check_cancelled():
q = stdlib_queue.Queue()
async def child(cancellable, scope):
with scope:
record.append("start")
try:
return await to_thread_run_sync(f, cancellable=cancellable)
except _core.Cancelled:
record.append("cancel")
raise
finally:
record.append("exit")
def f():
try:
from_thread_check_cancelled()
except _core.Cancelled: # pragma: no cover, test failure path
q.put("Cancelled")
else:
q.put("Not Cancelled")
ev.wait()
return from_thread_check_cancelled()
# Base case: nothing cancelled so we shouldn't see cancels anywhere
record = []
ev = threading.Event()
async with _core.open_nursery() as nursery:
nursery.start_soon(child, False, _core.CancelScope())
await wait_all_tasks_blocked()
assert record[0] == "start"
assert q.get(timeout=1) == "Not Cancelled"
ev.set()
# implicit assertion, Cancelled not raised via nursery
assert record[1] == "exit"
# cancellable=False case: a cancel will pop out but be handled by
# the appropriate cancel scope
record = []
ev = threading.Event()
scope = _core.CancelScope() # Nursery cancel scope gives false positives
async with _core.open_nursery() as nursery:
nursery.start_soon(child, False, scope)
await wait_all_tasks_blocked()
assert record[0] == "start"
assert q.get(timeout=1) == "Not Cancelled"
scope.cancel()
ev.set()
assert scope.cancelled_caught
assert "cancel" in record
assert record[-1] == "exit"
# cancellable=True case: slightly different thread behavior needed
# check thread is cancelled "soon" after abandonment
def f(): # noqa: F811
ev.wait()
try:
from_thread_check_cancelled()
except _core.Cancelled:
q.put("Cancelled")
else: # pragma: no cover, test failure path
q.put("Not Cancelled")
record = []
ev = threading.Event()
scope = _core.CancelScope()
async with _core.open_nursery() as nursery:
nursery.start_soon(child, True, scope)