vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.86k stars 2.17k forks source link

cgen: invalid memory access panic when trying cleanup threads #22776

Open enghitalo opened 2 weeks ago

enghitalo commented 2 weeks ago

Describe the bug

panic when trying cleanup threads

Reproduction Steps

v -gc none run v/bug/bug.v

import os
import time

fn C.pthread_cancel(_thread thread) int

fn C.pthread_join(_thread thread, retval &voidptr) int

const max_thread_pool_size = 16

fn cleanup(threads [max_thread_pool_size]thread) {
    println('Terminating...\n')
    lock {
        for i := 0; i < max_thread_pool_size; i++ {
            C.pthread_cancel(threads[i])
            C.pthread_join(threads[i], (unsafe { nil }))
        }
    }
    println('Server terminated')
    exit(0)
}

fn worker_thread() {
    println('worker thread started')
    for {
        time.sleep(1)
    }
}

fn main() {
    mut threads := [max_thread_pool_size]thread{}
    os.signal_opt(.int, fn [threads] (signum os.Signal) {
        cleanup(threads)
    })!
    os.signal_opt(.term, fn [threads] (signum os.Signal) {
        cleanup(threads)
    })!

    for i in 0 .. max_thread_pool_size {
        threads[i] = spawn worker_thread()
    }

    println('running')

    for {
        time.sleep(1)
    }
}

Expected Behavior

?

Current Behavior

0x788f0109a8fd: at ???: RUNTIME ERROR: invalid memory access
/tmp/v_1000/bug.01JC11HD4F80X43G4EW2VNQH5W.tmp.c:24984: by main__cleanup
/tmp/v_1000/bug.01JC11HD4F80X43G4EW2VNQH5W.tmp.c:4726: by anon_fn_4e6320ae8d4fa7b5_os__signal_612
0x788f01045320: by ???
0x788f010f9a27: by ???
/tmp/v_1000/bug.01JC11HD4F80X43G4EW2VNQH5W.tmp.c:18269: by time__sleep
/tmp/v_1000/bug.01JC11HD4F80X43G4EW2VNQH5W.tmp.c:25033: by main__main
/tmp/v_1000/bug.01JC11HD4F80X43G4EW2VNQH5W.tmp.c:25142: by main

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.8 744ac80

Environment details (OS name and version, etc.)

Linux

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21222

felipensp commented 2 weeks ago

Can you reproduce it consistently? I can't reproduce it.

JalonSolov commented 2 weeks ago

I tried with just v -g run bug.v, and got the invalid memory access message when I hit Ctrl-C:

[jalon@7950x ~]$ v -g run bug.v
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
worker thread started
running
^CTerminating...

0x745a5dc1c50d: at ???: RUNTIME ERROR: invalid memory access
/tmp/v_1000/../../../../../../home/jalon/bug.v:14: by main__cleanup
/tmp/v_1000/../../../../../../home/jalon/bug.v:32: by anon_fn_70a6b7e29084d475_os__signal_612
0x79b3a1da21d0: by ???
0x79b3a1e52827: by ???
/tmp/v_1000/../../../../../../home/jalon/git/v/vlib/time/time_nix.c.v:130: by time__sleep
/tmp/v_1000/../../../../../../home/jalon/bug.v:45: by main__main
/tmp/v_1000/../../../../../../tmp/v_1000/bug.01JC3GFV2GEW2NRVXK64P06FX2.tmp.c:48409: by main
[jalon@7950x ~]$

Looks like time.sleep() doesn't like to be interrupted...

felipensp commented 2 weeks ago

Ah sorry, I forgot about signal part. Thanks.