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

syscall: cannot use syscall.NewCallback for procs that run on new threads #20823

Closed
stevenh opened this issue Jun 28, 2017 · 2 comments
Closed

Comments

@stevenh
Copy link
Contributor

stevenh commented Jun 28, 2017

This is basically a duplicate of the frozen issue #9240

It is not possible to call Win32 API functions that spawn new threads because the callback wrapper generated by syscall.NewCallback does not perform the needed steps to set up the call stack as needed by the Go runtime for a new thread unless "C" is imported.

Attempting to call such functions will cause the new thread to hang forever.

This can be easily reproduced with the following simple test program:

package main

import (
    //"C" // Uncomment this and it works
    "fmt"
    "syscall"
)

func ThreadProc(p uintptr) uintptr {
    fmt.Println("FOO")
    return 0
}

func main() {
    modkernel32 := syscall.MustLoadDLL("kernel32.dll")
    procCreateThread := modkernel32.MustFindProc("CreateThread")
    r1, _, _ := procCreateThread.Call(0, 0, syscall.NewCallback(ThreadProc), 0, 0, 0)
    h := syscall.Handle(r1)
    syscall.WaitForSingleObject(h, syscall.INFINITE)
    syscall.CloseHandle(h)
}

I'm re-raising this as we burned may hours over the past few days identifying why Windows callbacks weren't working, which turned out to be this issue.

At the very least syscall.NewCallback(..) should have a warning about this issue, but ideally we should fix this.

@rsc suggestion of always enabling "C" under Windows seems reasonable, if its an option.

@bradfitz
Copy link
Contributor

#9240 was closed as a dup of #6751, which is still open. Is this not a dup of #6751 as well?

@stevenh
Copy link
Contributor Author

stevenh commented Jun 28, 2017

Sorry yes it Is I read, #9240 and the last thing was "gopherbot added the FrozenDueToAge label on Jun 25 2016" so I assumed it has been frozen without resolution, which is not the case. I'll add the repoduction case and info to #6751.

@stevenh stevenh closed this as completed Jun 28, 2017
@golang golang locked and limited conversation to collaborators Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants