summaryrefslogtreecommitdiff
path: root/thread.c
AgeCommit message (Collapse)Author
2023-04-04fix deadlock on `Thread#join`Koichi Sasada
because of 9720f5ac894566ade2aabcf9adea0a3235de1353 http://rubyci.s3.amazonaws.com/solaris11-sunc/ruby-master/log/20230403T130011Z.fail.html.gz ``` 1) Failure: TestThread#test_signal_at_join [/export/home/chkbuild/chkbuild-sunc/tmp/build/20230403T130011Z/ruby/test/ruby/test_thread.rb:1488]: Exception raised: <#<fatal:"No live threads left. Deadlock?\n1 threads, 1 sleeps current:0x00891288 main thread:0x00891288\n* #<Thread:0xfef89a18 sleep_forever>\n rb_thread_t:0x00891288 native:0x00000001 int:0\n \n">> Backtrace: -:30:in `join' -:30:in `block (3 levels) in <main>' -:21:in `times' -:21:in `block (2 levels) in <main>'. ``` The mechanism: * Main thread (M) calls `Thread#join` * M: calls `sleep_forever()` * M: set `th->status = THREAD_STOPPED_FOREVER` * M: do `checkints` * M: handle a trap handler with `th->status = THREAD_RUNNABLE` * M: thread switch at the end of the trap handler * Another thread (T) will process `Thread#kill` by M. * T: `rb_threadptr_join_list_wakeup()` at the end of T tris to wakeup M, but M's state is runnable because M is handling trap handler and just ignore the waking up and terminate T$a * T: switch to M. * M: after the trap handler, reset `th->status = THREAD_STOPPED_FOREVER` and check deadlock -> Deadlock because only M is living. To avoid such situation, add new sleep flags `SLEEP_ALLOW_SPURIOUS` and `SLEEP_NO_CHECKINTS` to skip any check ints. BTW this is instentional to leave second `vm_check_ints_blocking()` without checking `SLEEP_NO_CHECKINTS` because `SLEEP_ALLOW_SPURIOUS` should be specified with `SLEEP_NO_CHECKINTS` and skipping this checkints can skip any interrupts. Notes: Merged: https://github.com/ruby/ruby/pull/7647
2023-04-01use `sleep_forever()` on `thread_join_sleep()`Koichi Sasada
because it does same thing. Notes: Merged: https://github.com/ruby/ruby/pull/7642
2023-03-31cosmetic changeKoichi Sasada
reorder `sleep_forever()` and so on.
2023-03-31pass `th` to `thread_sched_to_waiting()`Koichi Sasada
for future extension Notes: Merged: https://github.com/ruby/ruby/pull/7639
2023-03-31remove "\n" for `RUBY_DEBUG_LOG()`Koichi Sasada
because `RUBY_DEBUG_LOG()` add "\n" at the end of message.
2023-03-30`rb_ractor_thread_list()` only for current ractorKoichi Sasada
so that no need to lock the ractor. Notes: Merged: https://github.com/ruby/ruby/pull/7616
2023-03-30cosmetic changeKoichi Sasada
Notes: Merged: https://github.com/ruby/ruby/pull/7618
2023-03-15Rename RB_GC_SAVE_MACHINE_CONTEXT -> RB_VM_SAVE_MACHINE_CONTEXTMatt Valentine-House
Notes: Merged: https://github.com/ruby/ruby/pull/7465
2023-03-15Remove SIGCHLD `waidpid`. (#7527)Samuel Williams
* Remove `waitpid_lock` and related code. * Remove un-necessary test. * Remove `rb_thread_sleep_interruptible` dead code. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-14Revert SIGCHLD changes to diagnose CI failures. (#7517)Samuel Williams
* Revert "Remove special handling of `SIGCHLD`. (#7482)" This reverts commit 44a0711eab7fbc71ac2c8ff489d8c53e97a8fe75. * Revert "Remove prototypes for functions that are no longer used. (#7497)" This reverts commit 4dce12bead3bfd91fd80b5e7195f7f540ffffacb. * Revert "Remove SIGCHLD `waidpid`. (#7476)" This reverts commit 1658e7d96696a656d9bd0a0c84c82cde86914ba2. * Fix change to rjit variable name. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-09Remove SIGCHLD `waidpid`. (#7476)Samuel Williams
* Remove `waitpid_lock` and related code. * Remove un-necessary test. * Remove `rb_thread_sleep_interruptible` dead code. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-07Remove obsoleted functions in rjit.cTakashi Kokubun
2023-03-07Get rid of MJIT's special forkTakashi Kokubun
2023-03-06s/mjit/rjit/Takashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7462
2023-03-06s/MJIT/RJIT/Takashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7462
2023-03-07Correctly clean up `keeping_mutexes` before resuming any other threads. (#7460)Samuel Williams
It's possible (but very rare) to have a race condition between setting `mutex->fiber = NULL` and `thread_mutex_remove(th, mutex)` which results in the following bug: ``` [BUG] invalid keeping_mutexes: Attempt to unlock a mutex which is not locked ``` Fixes <https://bugs.ruby-lang.org/issues/19480>. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-06Stop exporting symbols for MJITTakashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7459
2023-03-06TestThreadInstrumentation: emit the EXIT event soonerJean Boussier
``` 1) Failure: TestThreadInstrumentation#test_thread_instrumentation [/tmp/ruby/src/trunk-repeat20-asserts/test/-ext-/thread/test_instrumentation_api.rb:33]: Call counters[4]: [3, 4, 4, 4, 0]. Expected 0 to be > 0. ``` We fire the EXIT hook after the call to `thread_sched_to_dead` which mean another thread might be running before the `EXIT` hook have been executed. Notes: Merged: https://github.com/ruby/ruby/pull/7249
2023-02-09Merge gc.h and internal/gc.hMatt Valentine-House
[Feature #19425] Notes: Merged: https://github.com/ruby/ruby/pull/7273
2023-02-08Only emit circular dependency warning for owned thread shieldsJean byroot Boussier
[Bug #19415] If multiple threads attemps to load the same file concurrently it's not a circular dependency issue. So we check that the existing ThreadShield is owner by the current fiber before warning about circular dependencies. Notes: Merged: https://github.com/ruby/ruby/pull/7257
2023-02-06Revert "Only emit circular dependency warning for owned thread shields"Jean byroot Boussier
This reverts commit fa49651e05a06512e18ccb2f54a7198c9ff579de. Notes: Merged: https://github.com/ruby/ruby/pull/7256
2023-02-06Only emit circular dependency warning for owned thread shieldsJean Boussier
[Bug #19415] If multiple threads attemps to load the same file concurrently it's not a circular dependency issue. So we check that the existing ThreadShield is owner by the current fiber before warning about circular dependencies. Notes: Merged: https://github.com/ruby/ruby/pull/7252
2023-01-20Remove unused struct member thgroup->groupMatt Valentine-House
Notes: Merged: https://github.com/ruby/ruby/pull/7162
2022-12-01Introduce `Fiber#storage` for inheritable fiber-scoped variables. (#6612)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-11-16Using UNDEF_P macroS-H-GAMELINKS
Notes: Merged: https://github.com/ruby/ruby/pull/6721
2022-11-09Make pending_interrupt?(Exception) workYusuke Endoh
A patch from katsu (Katsuhiro Ueno) [Bug #19110] Notes: Merged: https://github.com/ruby/ruby/pull/6689
2022-10-20Avoid missed wakeup with fiber scheduler and Fiber.blocking. (#6588)Samuel Williams
* Ensure that blocked fibers don't prevent valid wakeups. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-10-07Add IO#timeout attribute and use it for blocking IO operations. (#5653)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-09-11MJIT: Do not hang after forking with threadsTakashi Kokubun
First, rb_mjit_fork should call rb_thread_atfork to stop threads after fork in the child process. Unfortunately, we cannot use rb_fork_ruby to prevent this kind of mistakes because MJIT needs special handling of waiting_pid and mjit_pause/resume. Second, mjit_waitpid_finished should be checked regardless of trap_interrupt. It doesn't seem like the flag is not set when SIGCHLD is handled for an MJIT child process.
2022-09-07Exit status macros need sys/wait.h on FreeBSDNobuyoshi Nakada
2022-09-06Do not fork the process on --mjit-waitTakashi Kokubun
fork is for parallel compilation, but --mjit-wait cancels it. It's more useful to not fork it for binding.irb, debugging, etc.
2022-08-06Allow `RUBY_DEBUG_LOG` format to be emptyNobuyoshi Nakada
GCC warns of empty format strings, perhaps because they have no effects in printf() and there are better ways than sprintf(). However, ruby_debug_log() adds informations other than the format, this warning is not the case.
2022-08-02Implement Queue#pop(timeout: sec)Jean Boussier
[Feature #18774] As well as `SizedQueue#pop(timeout: sec)` If both `non_block=true` and `timeout:` are supplied, ArgumentError is raised. Notes: Merged: https://github.com/ruby/ruby/pull/6185
2022-07-26Rename rb_ary_tmp_new to rb_ary_hidden_newPeter Zhu
rb_ary_tmp_new suggests that the array is temporary in some way, but that's not true, it just creates an array that's hidden and not on the transient heap. This commit renames it to rb_ary_hidden_new. Notes: Merged: https://github.com/ruby/ruby/pull/6180
2022-07-21Expand tabs [ci skip]Takashi Kokubun
[Misc #18891] Notes: Merged: https://github.com/ruby/ruby/pull/6094
2022-07-13GVL Instrumentation: remove the EXITED count assertionJean Boussier
It's very flaky for some unknown reason. Something we have an extra EXITED event. I suspect some other test is causing this. Notes: Merged: https://github.com/ruby/ruby/pull/6133
2022-07-12Use VM Lock when mutating waiting threads listAaron Patterson
`rb_thread_wait_for_single_fd` needs to mutate the `waiting_fds` list that is stored on the VM. We need to delete the FD from the list before returning, and deleting from the list requires a VM lock (because the list is a global). [Bug #18816] [ruby-core:108771] Co-Authored-By: Alan Wu <alanwu@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/6116
2022-07-07Thread#value: handle threads killed by a forkJean Boussier
[Bug #18902] When a thread is killed because we forked, the `value` if left to `Qundef`. Returning it woudl crash the VM. Notes: Merged: https://github.com/ruby/ruby/pull/6103
2022-06-17GVL Instrumentation API: add STARTED and EXITED eventsJean Boussier
[Feature #18339] After experimenting with the initial version of the API I figured there is a need for an exit event to cleanup instrumentation data. e.g. if you record data in a {thread_id -> data} table, you need to free associated data when a thread goes away. Notes: Merged: https://github.com/ruby/ruby/pull/6029
2022-06-15Restore rb_exec_recursive_outerJohn Hawthorn
This was a public method, so we should probably keep it. Notes: Merged: https://github.com/ruby/ruby/pull/6027
2022-06-15Remove MJIT worker thread (#6006)Takashi Kokubun
[Misc #18830] Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2022-06-10Make method id explicit in rb_exec_recursive_outerJohn Hawthorn
Previously, because opt_aref and opt_aset don't push a frame, when they would call rb_hash to determine the hash value of the key, the initial level of recursion would incorrectly use the method id at the top of the stack instead of "hash". This commit replaces rb_exec_recursive_outer with rb_exec_recursive_outer_mid, which takes an explicit method id, so that we can make the hash calculation behave consistently. rb_exec_recursive_outer was documented as being internal, so I believe this should be okay to change. Notes: Merged: https://github.com/ruby/ruby/pull/6004
2022-06-10Add mid argument to exec_recursiveJohn Hawthorn
Notes: Merged: https://github.com/ruby/ruby/pull/6004
2022-05-24use `RUBY_DEBUG_LOG` instead of `thread_debug`Koichi Sasada
`thread_debug()` was introduced to print debug messages on `THREAD_DEBUG > 0` but `RUBY_DEBUG_LOG()` is more controllable. Notes: Merged: https://github.com/ruby/ruby/pull/5933
2022-05-24remove `NON_SCALAR_THREAD_ID` supportKoichi Sasada
`NON_SCALAR_THREAD_ID` shows `pthread_t` is non-scalar (non-pointer) and only s390x is known platform. However, the supporting code is very complex and it is only used for deubg print information. So this patch removes the support of `NON_SCALAR_THREAD_ID` and make the code simple. Notes: Merged: https://github.com/ruby/ruby/pull/5933
2022-05-17Delete autoload data from global features after autoload has completed. (#5910)Samuel Williams
* Update naming of critical section assertions macros. * Improved locking for autoload. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-05-15Fix various autoload race conditions. (#5898)Samuel Williams
* Add RUBY_VM_CRITICAL_SECTION for detecting unexpected context switch. * Prevent race between GC mark and autoload setup. * Protect race on autoload state. * Avoid potential race condition when allocating `autoload_featuremap`. * Add NEWS entry for autoload fixes. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-04-23introduce struct `rb_native_thread`Koichi Sasada
`rb_thread_t` contained `native_thread_data_t` to represent thread implementation dependent data. This patch separates them and rename it `rb_native_thread` and point it from `rb_thraed_t`. Now, 1 Ruby thread (`rb_thread_t`) has 1 native thread (`rb_native_thread`). Notes: Merged: https://github.com/ruby/ruby/pull/5836
2022-04-22rename thread internal namingKoichi Sasada
Now GVL is not process *Global* so this patch try to use another words. * `rb_global_vm_lock_t` -> `struct rb_thread_sched` * `gvl->owner` -> `sched->running` * `gvl->waitq` -> `sched->readyq` * `rb_gvl_init` -> `rb_thread_sched_init` * `gvl_destroy` -> `rb_thread_sched_destroy` * `gvl_acquire` -> `thread_sched_to_running` # waiting -> ready -> running * `gvl_release` -> `thread_sched_to_waiting` # running -> waiting * `gvl_yield` -> `thread_sched_yield` * `GVL_UNLOCK_BEGIN` -> `THREAD_BLOCKING_BEGIN` * `GVL_UNLOCK_END` -> `THREAD_BLOCKING_END` * removed * `rb_ractor_gvl` * `rb_vm_gvl_destroy` (not used) There are GVL functions such as `rb_thread_call_without_gvl()` yet but I don't have good name to replace them. Maybe GVL stands for "Greate Valuable Lock" or something like that. Notes: Merged: https://github.com/ruby/ruby/pull/5814
2022-04-14[DOC] Fix SymbolVariantsRelNotes linkS-H-GAMELINKS
Notes: Merged: https://github.com/ruby/ruby/pull/5682