Odin icon indicating copy to clipboard operation
Odin copied to clipboard

`core:thread#run` doesn't properly destroy its own thread

Open ftphikari opened this issue 1 year ago • 0 comments

Context

  • Operating System & Odin Version:

      Odin: dev-2023-03:
      OS:   Windows 11 Professional (version: 22H2), build 22621.1343
      CPU:  Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
      RAM:  16263 MiB
    

Currently thread.run invokes a function:

	thread_proc :: proc(t: ^Thread) {
		fn := cast(proc(rawptr))t.data
		assert(t.user_index >= 1)
		data := t.user_args[0]
		fn(data)
		destroy(t)
	}

Which is supposed to destroy the thread when it finishes working. But in reality thread just blocks inside destroy, inside _join, since it tries to join it to itself.

Minimal example

package bug

import "core:fmt"
import "core:time"
import "core:mem"
import "core:thread"

main :: proc() {
	tracking_allocator: mem.Tracking_Allocator
	mem.tracking_allocator_init(&tracking_allocator, context.allocator)
	context.allocator = mem.tracking_allocator(&tracking_allocator)

	dud :: proc() {
		fmt.println("finished")
	}
	thread.run(dud)

	time.sleep(2 * time.Second)

	for _, leak in tracking_allocator.allocation_map {
		fmt.printf("%v leaked %v bytes\n", leak.location, leak.size)
	}
}

Expected output

Nothing.

Actual output

Odin/core/thread/thread_windows.odin(51:12) leaked 248 bytes If thread was destroyed successfully, it would have freed itself.

ftphikari avatar Mar 11 '23 09:03 ftphikari