Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the number of OS threads spawned by go process never decrease?

Consider a golang program running on a system with GOMAXPROCS value 10. Due to blocking system calls, OS spawns 30 more threads resulting in 40 OS threads attached to the process.

After all the blocked system calls returns, then will the process still be having 40 OS threads? If yes, then can we conclude that the number of OS threads mapped to a golang process can grow but never comes down?

like image 591
Varun V Avatar asked Jun 27 '26 15:06

Varun V


1 Answers

Yes, currently threads spawned due to blocked goroutines are not stopped. There's a discussion about closing idle threads periodically: runtime: let idle OS threads exit #14592

There is a way to kill a thread though. If you call runtime.LockOSThread() in a goroutine without calling its counterpart runtime.UnlockOSThread(), as per the doc:

If the calling goroutine exits without unlocking the thread, the thread will be terminated.

You may also do it using (source: runtime: terminate locked OS thread if its goroutine exits #20395):

syscall.Syscall(syscall.SYS_EXIT, 0, 0, 0)
like image 127
icza Avatar answered Jun 30 '26 07:06

icza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!