Skip to content

Thread didn't execute? #6142

@ioquatix

Description

@ioquatix
koyoko% jruby -v             
jruby 9.2.9.0 (2.5.7) 2019-10-30 458ad3e OpenJDK 64-Bit Server VM 25.242-b08 on 1.8.0_242-b08 +jit [linux-x86_64]
koyoko% uname -a
Linux koyoko.local 5.5.9-arch1-2 #1 SMP PREEMPT Thu, 12 Mar 2020 23:01:33 +0000 x86_64 GNU/Linux

In my live stream, talking about thread safety no less, I encountered some kind of issue in JRuby when using threads in code that should have been fine.

https://youtu.be/Wu9LRNOc5pQ?t=630

It appears that while we did construct a thread, it never actually executed any code.

Here is the code from the live stream above:

#!/usr/bin/env ruby

def test(repeats = 1000)
	mutex = Mutex.new
	
	mutexes = {}
	threads = []
	
	threads = repeats.times.map do |i|
		Thread.new do
			mutex.synchronize do
				mutexes[i] ||= Mutex.new
			end
		end
	end
	
	threads.each(&:join)
	
	if threads.size != mutexes.size
		puts "There were #{threads.size} threads and #{mutexes.size} mutexes."
		
		repeats.times do |i|
			unless mutexes.include?(i)
				puts "Missing #{i}!"
			end
		end
	end
	
	missing = repeats.times.to_a - mutexes.keys
	
	unless missing.empty?
		puts "Missing: #{missing.inspect}"
	end
end

100.times{test}

I have been discussing the issue with @headius and he suggested it might be a problem with the native thread being GCed before starting:

    public void start() {
        Thread thread = getThread();
        if (thread != null) thread.start();
        else throw new RuntimeException("WTF");
    }

and he was able to get it to fail by increasing GC frequency.

[] ~/projects/jruby $ jruby thread_mutexes.rb 
Unhandled Java exception: java.lang.RuntimeException: WTF

However I am not able to reproduce the issue, it seems very infrequent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions