Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
enghitalo opened this issue Nov 6, 2024 · 3 comments
Open

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

enghitalo opened this issue Nov 6, 2024 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Nov 6, 2024

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

@enghitalo enghitalo added the Bug This tag is applied to issues which reports bugs. label Nov 6, 2024
@felipensp
Copy link
Member

felipensp commented Nov 7, 2024

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

@JalonSolov
Copy link
Contributor

JalonSolov commented Nov 7, 2024

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...

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Nov 7, 2024
@felipensp
Copy link
Member

Ah sorry, I forgot about signal part. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor.
Projects
None yet
Development

No branches or pull requests

3 participants