<feed xmlns='http://www.w3.org/2005/Atom'>
<title>ruby.git/thread_pthread.c, branch v4.0.3</title>
<subtitle>The Ruby Programming Language</subtitle>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/'/>
<entry>
<title>merge revision(s) 08372635f7ec09f7115bd254246ebd637499651c: [Backport #21926]</title>
<updated>2026-03-16T18:57:04+00:00</updated>
<author>
<name>Takashi Kokubun</name>
<email>takashikkbn@gmail.com</email>
</author>
<published>2026-03-16T18:57:04+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=a601b899a35c796775309dca01a6d5e64be14c44'/>
<id>a601b899a35c796775309dca01a6d5e64be14c44</id>
<content type='text'>
	Fix race condition right after ubf registration

	Fixes [Bug #21926]
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	Fix race condition right after ubf registration

	Fixes [Bug #21926]
</pre>
</div>
</content>
</entry>
<entry>
<title>merge revision(s) 7e81bf5c0c8f43602e6d901f4253dca2f3d71745: [Backport #21812]</title>
<updated>2026-01-13T01:14:52+00:00</updated>
<author>
<name>Takashi Kokubun</name>
<email>takashikkbn@gmail.com</email>
</author>
<published>2026-01-13T01:14:52+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=ac596948d4008c6e117449786c62de4e45e434bf'/>
<id>ac596948d4008c6e117449786c62de4e45e434bf</id>
<content type='text'>
	[PATCH] Fix sleep spurious wakeup from sigchld (#15802)

	When sleeping with `sleep`, currently the main thread can get woken up from sigchld
	from any thread (subprocess exited). The timer thread wakes up the main thread when this
	happens, as it checks for signals. The main thread then executes the ruby sigchld handler
	if one is registered and is supposed to go back to sleep immediately. This is not ideal but
	it's the way it's worked for a while. In commit 8d8159e7d8 I added writes to `th-&gt;status`
	before and after `wait_running_turn` in `thread_sched_to_waiting_until_wakeup`, which is
	called from `sleep`. This is usually the right way to set the thread's status, but `sleep`
	is an exception because the writes to `th-&gt;status` are done in `sleep_forever`. There's a
	loop that checks `th-&gt;status` in `sleep_forever`. When the main thread got woken up from
	sigchld it saw the changed `th-&gt;status` and continued to run the main thread instead of
	going back to sleep.

	The following script shows the error. It was returning instead of sleeping forever.

	```ruby
	t = Thread.new do
	  sleep 0.3
	  `echo hello`  # Spawns subprocess
	  puts "Subprocess exited"
	end

	puts "Main thread sleeping..."
	result = sleep  # Should block forever
	puts "sleep returned: #{result.inspect}"
	```

	Fixes [Bug #21812]
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
	[PATCH] Fix sleep spurious wakeup from sigchld (#15802)

	When sleeping with `sleep`, currently the main thread can get woken up from sigchld
	from any thread (subprocess exited). The timer thread wakes up the main thread when this
	happens, as it checks for signals. The main thread then executes the ruby sigchld handler
	if one is registered and is supposed to go back to sleep immediately. This is not ideal but
	it's the way it's worked for a while. In commit 8d8159e7d8 I added writes to `th-&gt;status`
	before and after `wait_running_turn` in `thread_sched_to_waiting_until_wakeup`, which is
	called from `sleep`. This is usually the right way to set the thread's status, but `sleep`
	is an exception because the writes to `th-&gt;status` are done in `sleep_forever`. There's a
	loop that checks `th-&gt;status` in `sleep_forever`. When the main thread got woken up from
	sigchld it saw the changed `th-&gt;status` and continued to run the main thread instead of
	going back to sleep.

	The following script shows the error. It was returning instead of sleeping forever.

	```ruby
	t = Thread.new do
	  sleep 0.3
	  `echo hello`  # Spawns subprocess
	  puts "Subprocess exited"
	end

	puts "Main thread sleeping..."
	result = sleep  # Should block forever
	puts "sleep returned: #{result.inspect}"
	```

	Fixes [Bug #21812]
</pre>
</div>
</content>
</entry>
<entry>
<title>move th-&gt;event_serial to rb_thread_sched_item (#15500)</title>
<updated>2025-12-12T19:24:40+00:00</updated>
<author>
<name>Luke Gruber</name>
<email>luke.gruber@shopify.com</email>
</author>
<published>2025-12-12T19:24:40+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=7909ce2a839ba1c3e134239189e6aa2de3b6b630'/>
<id>7909ce2a839ba1c3e134239189e6aa2de3b6b630</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>Simplify the code</title>
<updated>2025-12-12T09:46:06+00:00</updated>
<author>
<name>Koichi Sasada</name>
<email>ko1@atdot.net</email>
</author>
<published>2025-12-12T09:14:50+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=d428d086c23219090d68eb2d027498c6ea999b89'/>
<id>d428d086c23219090d68eb2d027498c6ea999b89</id>
<content type='text'>
`thread_sched_to_waiting_common0` is no longer needed.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
`thread_sched_to_waiting_common0` is no longer needed.
</pre>
</div>
</content>
</entry>
<entry>
<title>Fix typo in thread_pthread.c [ci skip] (#15465)</title>
<updated>2025-12-09T23:28:18+00:00</updated>
<author>
<name>Yuji Teshima</name>
<email>36704166+yujiteshima@users.noreply.github.com</email>
</author>
<published>2025-12-09T23:28:18+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=264c469bc2cd6a9320cb1545d83ebda69e421f49'/>
<id>264c469bc2cd6a9320cb1545d83ebda69e421f49</id>
<content type='text'>
Fix typo in thread_pthread.c: threre -&gt; there</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Fix typo in thread_pthread.c: threre -&gt; there</pre>
</div>
</content>
</entry>
<entry>
<title>Fix thread scheduler issue with thread_sched_wait_events (#15392)</title>
<updated>2025-12-04T21:51:11+00:00</updated>
<author>
<name>Luke Gruber</name>
<email>luke.gruber@shopify.com</email>
</author>
<published>2025-12-04T21:51:11+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=8d8159e7d87e4fd1594ce2fad3d2653e47fb1026'/>
<id>8d8159e7d87e4fd1594ce2fad3d2653e47fb1026</id>
<content type='text'>
Fix race between timer thread dequeuing waiting thread and thread
skipping sleeping due to being dequeued. We now use `th-&gt;event_serial` which
is protected by `thread_sched_lock`. When a thread is put on timer thread's waiting
list, the event serial is saved on the item. The timer thread checks
that the saved serial is the same as current thread's serial before
calling `thread_sched_to_ready`.

The following script (taken from a test in `test_thread.rb` used to crash on
scheduler debug assertions. It would likely crash in non-debug mode as well.

```ruby
def assert_nil(val)
  if val != nil
    raise "Expected #{val} to be nil"
  end
end

def assert_equal(expected, actual)
  if expected != actual
    raise "Expected #{expected} to be #{actual}"
  end
end

def test_join2
  ok = false
  t1 = Thread.new { ok = true; sleep }
  Thread.pass until ok
  Thread.pass until t1.stop?
  t2 = Thread.new do
    Thread.pass while ok
    t1.join(0.01)
  end
  t3 = Thread.new do
    ok = false
    t1.join
  end
  assert_nil(t2.value)
  t1.wakeup
  assert_equal(t1, t3.value)
ensure
  t1&amp;.kill&amp;.join
  t2&amp;.kill&amp;.join
  t3&amp;.kill&amp;.join
end

rs = 30.times.map do
  Ractor.new do
    test_join2
  end
end
rs.each(&amp;:join)
```</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
Fix race between timer thread dequeuing waiting thread and thread
skipping sleeping due to being dequeued. We now use `th-&gt;event_serial` which
is protected by `thread_sched_lock`. When a thread is put on timer thread's waiting
list, the event serial is saved on the item. The timer thread checks
that the saved serial is the same as current thread's serial before
calling `thread_sched_to_ready`.

The following script (taken from a test in `test_thread.rb` used to crash on
scheduler debug assertions. It would likely crash in non-debug mode as well.

```ruby
def assert_nil(val)
  if val != nil
    raise "Expected #{val} to be nil"
  end
end

def assert_equal(expected, actual)
  if expected != actual
    raise "Expected #{expected} to be #{actual}"
  end
end

def test_join2
  ok = false
  t1 = Thread.new { ok = true; sleep }
  Thread.pass until ok
  Thread.pass until t1.stop?
  t2 = Thread.new do
    Thread.pass while ok
    t1.join(0.01)
  end
  t3 = Thread.new do
    ok = false
    t1.join
  end
  assert_nil(t2.value)
  t1.wakeup
  assert_equal(t1, t3.value)
ensure
  t1&amp;.kill&amp;.join
  t2&amp;.kill&amp;.join
  t3&amp;.kill&amp;.join
end

rs = 30.times.map do
  Ractor.new do
    test_join2
  end
end
rs.each(&amp;:join)
```</pre>
</div>
</content>
</entry>
<entry>
<title>Fix `thread_sched_wait_events` race (#15067)</title>
<updated>2025-11-11T01:32:30+00:00</updated>
<author>
<name>Luke Gruber</name>
<email>luke.gruber@shopify.com</email>
</author>
<published>2025-11-11T01:32:30+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=d2c30a3bae908772c1de453aad8686000f6a5096'/>
<id>d2c30a3bae908772c1de453aad8686000f6a5096</id>
<content type='text'>
This race condition was found when calling `Thread#join` with a timeout
inside a ractor. The race is between the polling thread waking up the
thread and the `ubf` getting called (`ubf_event_waiting`). The error was
that the ubf or polling thread would set the thread as ready, but then
the other function would do the same.

Fixes [Bug #21614]</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This race condition was found when calling `Thread#join` with a timeout
inside a ractor. The race is between the polling thread waking up the
thread and the `ubf` getting called (`ubf_event_waiting`). The error was
that the ubf or polling thread would set the thread as ready, but then
the other function would do the same.

Fixes [Bug #21614]</pre>
</div>
</content>
</entry>
<entry>
<title>mn timer thread: force wakeups for timeouts</title>
<updated>2025-10-30T20:44:25+00:00</updated>
<author>
<name>Andre Muta</name>
<email>andremuta@gmail.com</email>
</author>
<published>2025-10-30T03:55:40+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=0531fa4d6fea100f69f0bac9e03973fe49ecd570'/>
<id>0531fa4d6fea100f69f0bac9e03973fe49ecd570</id>
<content type='text'>
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
</pre>
</div>
</content>
</entry>
<entry>
<title>Add debug #define to call sched_yield before each pthread_mutex_lock</title>
<updated>2025-10-07T20:00:16+00:00</updated>
<author>
<name>Luke Gruber</name>
<email>luke.gruber@shopify.com</email>
</author>
<published>2025-10-07T14:28:37+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=446257c84b92c63d84282eadca32b56ed1281a3d'/>
<id>446257c84b92c63d84282eadca32b56ed1281a3d</id>
<content type='text'>
This is useful for debugging mutex issues as it increases contention for locks.
It is off by default.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
This is useful for debugging mutex issues as it increases contention for locks.
It is off by default.
</pre>
</div>
</content>
</entry>
<entry>
<title>Set context_stack on main thread</title>
<updated>2025-09-30T13:47:29+00:00</updated>
<author>
<name>Peter Zhu</name>
<email>peter@peterzhu.ca</email>
</author>
<published>2025-09-29T22:33:04+00:00</published>
<link rel='alternate' type='text/html' href='https://git.ruby-lang.org/ruby.git/commit/?id=d8c8623f50af8f5324e1679ff95b1a2071c0c61e'/>
<id>d8c8623f50af8f5324e1679ff95b1a2071c0c61e</id>
<content type='text'>
We allocate the stack of the main thread using malloc, but we never set
malloc_stack to true and context_stack. If we fork, the main thread may
no longer be the main thread anymore so it reports memory being leaked
in RUBY_FREE_AT_EXIT.

This commit allows the main thread to free its own VM stack at shutdown.
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
We allocate the stack of the main thread using malloc, but we never set
malloc_stack to true and context_stack. If we fork, the main thread may
no longer be the main thread anymore so it reports memory being leaked
in RUBY_FREE_AT_EXIT.

This commit allows the main thread to free its own VM stack at shutdown.
</pre>
</div>
</content>
</entry>
</feed>
