31 Commits

Author SHA1 Message Date
Jeong, YunWon
45d81296e4 More fork safety (#7380)
* apply more allow_threads

* Simplify STW thread state transitions

- Fix park_detached_threads: successful CAS no longer sets
  all_suspended=false, avoiding unnecessary polling rounds
- Replace park_timeout(50µs) with park() in wait_while_suspended
- Remove redundant self-suspension in attach_thread and detach_thread;
  the STW controller handles DETACHED→SUSPENDED via park_detached_threads
- Add double-check under mutex before condvar wait to prevent lost wakes
- Remove dead stats_detach_wait_yields field and add_detach_wait_yields

* Representable for ThreadHandle

* Set ThreadHandle state to Running in parent thread after spawn

Like CPython's ThreadHandle_start, set RUNNING state in the parent
thread immediately after spawn() succeeds, rather than in the child.
This eliminates a race where join() could see Starting state if called
before the child thread executes.

Also reverts the macOS skip for test_start_new_thread_failed since the
root cause is fixed.

* Set ThreadHandle state to Running in parent thread after spawn

* Add debug_assert for thread state in start_the_world

* Unskip now-passing test_get_event_loop_thread and test_start_new_thread_at_finalization

* Wrap IO locks and file ops in allow_threads

Add lock_wrapped to ThreadMutex for detaching thread state
while waiting on contended locks. Use it for buffered and
text IO locks. Wrap FileIO read/write in allow_threads via
crt_fd to prevent STW hangs on blocking file operations.

* Use std::sync for thread start/ready events

Replace parking_lot Mutex/Condvar with std::sync (pthread-based)
for started_event and handle_ready_event. This prevents hangs
in forked children where parking_lot's global HASHTABLE may be
corrupted.
2026-03-08 18:06:23 +09:00
Jeong, YunWon
375b5472ed Fix thread-safety in GC, type cache, and instruction cache (#7355)
* Fix thread-safety in GC, type cache, and instruction cache

GC / refcount:
- Add safe_inc() check for strong()==0 in RefCount
- Add try_to_owned() to PyObject for atomic refcount acquire
- Replace strong_count()+to_owned() with try_to_owned() in GC
  collection and weakref callback paths to prevent TOCTOU races

Type cache:
- Add proper SeqLock (sequence counter) to TypeCacheEntry
- Readers spin-wait on odd sequence, validate after read
- Writers bracket updates with begin_write/end_write
- Use try_to_owned + pointer revalidation on read path
- Call modified() BEFORE attribute modification in set_attr

Instruction cache:
- Add pointer_cache (AtomicUsize array) to CodeUnits for
  single atomic pointer load/store (prevents torn reads)
- Add try_read_cached_descriptor with try_to_owned + pointer
  and version revalidation after increment
- Add write_cached_descriptor with version-bracketed writes

RLock:
- Fix release() to check is_owned_by_current_thread
- Add _release_save/_acquire_restore methods

* Fix RLock _acquire_restore tuple handling and unxfail threading test

* Align type cache seqlock writer protocol with CPython

* RLock: use single parking_lot level, track recursion manually

Instead of calling lock()/unlock() N times for recursion depth N,
keep parking_lot at 1 level and manage the count ourselves.
This makes acquire/release O(1) and matches CPython's
_PyRecursiveMutex approach (lock once + set level directly).

* Add try_to_owned_from_ptr to avoid &PyObject on stale ptrs

Use addr_of! to access ref_count directly from a raw pointer
without forming &PyObject first. Applied in type cache and
instruction cache hit paths where the pointer may be stale.

* Fix CI: spelling typo and xfail flaky test_thread_safety

- Fix "minimising" -> "minimizing" for cspell
- xfail test_thread_safety: dict iteration races with
  concurrent GC mutations in _finalizer_registry
2026-03-05 20:33:14 +09:00
Jeong, YunWon
6950baf687 more algorithm-independent GC infra (#7194)
* mark poluting tests

* GC-infra independent to EBR

* trashcan

* add overflow guard to inc(), #[must_use] on dec()/safe_inc(), trashcan debug_assert, weakref generic re-check
2026-02-22 21:31:42 +09:00
Jeong, YunWon
4f366d7937 Skip flaky test_main_thread_after_fork_from_nonmain_thread 2026-02-15 23:42:09 +09:00
Jeong, YunWon
d2d8eeea2f Add _types module (#6807)
* _types

* builtin_function_or_method

* PyCapsule

* function_or_method

* Remove expectedFailure for test_builtin_function and test_join_nondaemon_on_shutdown
2026-02-08 19:17:03 +09:00
Terry Tianlin Luan
609dbb1439 Downgraded skips in tests (#6716)
* Downgraded skips in tests

* Fixed failing tests

* Fixed test_ftplib + test_socket + test_ssl + test_threaded_import failures

* Removed comments about which tests are run in which environment

* Addressed PR comments

* Annotated skips on failing tests

* Removed unneeded tests

* Removed unneeded sys import from test_ftplib

* Added annotation to test_ftplib

* Readded skipIf to test_cleanup_with_symlink_modes with a more general ENV_POLLUTING_TESTS_WINDOWS

* Addressed PR comments

* Made changes to minimize diff in PR

* Apply suggestion from @youknowone

---------

Co-authored-by: Jeong, YunWon <69878+youknowone@users.noreply.github.com>
2026-01-15 21:43:22 +09:00
Jeong, YunWon
69601a18a4 Upgrade threading to 3.13.11; sys.setprofile & impl more threading (#6691)
* Upgrade threading,test_threading from 3.13.11

* increase ci limit to 45mins

* impl more threading

* no static for asyncgen
2026-01-12 15:33:28 +09:00
Padraic Fanning
d1e75c0ee0 Make env polluter skip in test_threading os-agnostic 2026-01-10 22:10:35 -05:00
Padraic Fanning
b76c0a20a8 Skip certain problematic tests in test_threading 2026-01-10 22:10:35 -05:00
Padraic Fanning
17cc559dd3 Skip more flaky tests in test_threading 2026-01-10 22:10:35 -05:00
Copilot
e367145a4a Add Linux pidfd syscalls to expose asyncio pidfd watcher (#6660)
* Add pidfd support syscalls

* Validate pidfd signal range

* Clarify pidfd syscall return handling

* Skip flaky is_alive_after_fork test

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-01-06 15:10:23 +09:00
Gyubong Lee
ce5e1bd455 Update threading lib and tests from CPython 3.12.0a0 (#4156)
* Update threading.py from CPython v3.12.0a0

* Update test_threading.py from CPython v3.12.0a0

* mark failing tests

---------

Co-authored-by: Jeong YunWon <jeong@youknowone.org>
2026-01-05 08:01:00 +09:00
Jeong YunWon
d919fe516e mark test_threading 2025-12-23 11:55:40 +09:00
Jeong, YunWon
0624ca622b functools.partial (#5825)
* functools.partial

* apply review
2025-06-23 23:53:57 +09:00
Jeong, YunWon
5d68313f91 sys.setswitchinterval (#5817) 2025-06-22 22:36:13 +09:00
Ankit Kumar Pandey
058f8c5500 Add fork support functions (#4972) 2023-06-01 15:29:16 +09:00
Ankit Kumar Pandey
322aa6887a add support for os.fork and related functions (#4877) 2023-04-25 02:39:54 +09:00
Jeong YunWon
413a668582 Mark failing tests from test_threading 2022-08-23 15:54:42 +09:00
CPython developers
a1548e20f5 Update thread/threading from CPython 3.10.5 2022-08-23 04:46:07 +09:00
Karatus
b1478a68b5 Feature: _thread.interrupt_main (#4082) 2022-08-17 16:10:33 +09:00
Padraic Fanning
edbc17fb38 Skip flaky test 2022-02-25 19:33:53 -05:00
Padraic Fanning
c8748bc98e Fix test.support imports 2022-02-13 17:56:08 -05:00
Dean Li
6f98288e84 test: use import_helper 2021-11-29 21:03:02 +08:00
Dean Li
5ee4fb899b test: use os_helper 2021-11-28 20:51:32 +08:00
Jeong YunWon
913b78ca44 Revert "Merge pull request #3433 from deantvv/test-update"
This reverts commit 9fa5c5ac66, reversing
changes made to e7fa32c687.
2021-11-17 17:06:51 +09:00
Dean Li
49a5805d11 test: use os_helper 2021-11-13 02:18:33 +00:00
jfh
01e5b12290 Bump recursion limit to match CPython's 2021-08-27 09:29:52 +03:00
Padraic Fanning
5692639d99 Skip flaky threading test 2021-07-08 17:30:31 -04:00
Padraic Fanning
8727919735 Skip flaky threading test 2021-06-09 17:46:15 -04:00
Padraic Fanning
4f375c3fb8 Mark erroring/failing tests 2021-05-23 17:37:12 -04:00
Padraic Fanning
1d1337c954 Add test_threading from CPython 3.8 2021-05-23 17:37:11 -04:00