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

plugin: using plugin in go routine is not working #19145

Closed
Nhoya opened this issue Feb 17, 2017 · 3 comments
Closed

plugin: using plugin in go routine is not working #19145

Nhoya opened this issue Feb 17, 2017 · 3 comments

Comments

@Nhoya
Copy link

Nhoya commented Feb 17, 2017

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

1.8

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/nhoya/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build587728410=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

plug, err := plugin.Open(pluginsDir + "/" + p + ".so")
utils.CheckErrors(err)
S, err := plug.Lookup("Start_"+p)
utils.CheckErrors(err)
go  S.(func())() 

is printing nothing
while whitout the goroutine it is

plug, err := plugin.Open(pluginsDir + "/" + p + ".so")
utils.CheckErrors(err)
S, err := plug.Lookup("Start_"+p)
utils.CheckErrors(err)
 S.(func())() 

What did you expect to see?

I expect to see printed "Hello World"

What did you see instead?

I don't see anything

@ianlancetaylor ianlancetaylor changed the title Using plugin in go routine is not working plugin: using plugin in go routine is not working Feb 17, 2017
@ianlancetaylor
Copy link
Contributor

Please show us a complete example. Thanks.

@Nhoya
Copy link
Author

Nhoya commented Feb 17, 2017

Main

func main() {
	plugins := make([]string, 0)

	pluginsDir := "./plugins"
	files, _ := ioutil.ReadDir(pluginsDir)
    for _, f := range files {
    	if strings.HasSuffix(f.Name(), ".so") {
    		name := f.Name()[:len(f.Name())-3]
            plugins = append(plugins, name)
    	}
    }
    plugins = []string{"Helloworld"}
    fmt.Println("Loaded plugins:",plugins)

    for _, p := range plugins {
    	fmt.Println("Opening",p)
    	plug, err := plugin.Open(pluginsDir + "/" + p + ".so")
		utils.CheckErrors(err)
		S, err := plug.Lookup("Start_"+p)
		utils.CheckErrors(err)
		go func (){ S.(func())() }
    } 
	
}

plugins/Helloworld.so

package main

import "fmt"


func Start_Helloworld(){
	fmt.Println("HelloWorld")
}

@ianlancetaylor
Copy link
Contributor

You aren't waiting for the goroutine to complete. Your program starts the goroutines and then immediately returns from main and exits before the goroutines have a chance to do anything. You need to use a sync.WaitGroup or some other synchronization mechanism to let the goroutines complete before you return from main.

Closing.

@golang golang locked and limited conversation to collaborators Feb 17, 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