Why isn't my function returning?

blz

Below is a function that starts an external process, matches a regular expression against the process' standard output, and returns the contents of the match.

func (c *Colony) startCircuit(peer *string) (string, error) {
    var (
        err        error
        cmd        *exec.Cmd
        anchorChan chan string
    )

    // ... (omitted for readability)

    // get the anchor from standard output
    go func() {
        defer out.Close()

        anchorChan = make(chan string)
        for scanner := bufio.NewScanner(out); scanner.Scan(); {
            line := scanner.Text()
            if anchor := reRootAnchor.FindString(line); anchor != "" {
                log.Println("Started circuit server with anchor:", anchor)
                anchorChan <- anchor
                break
            }
        }
    }()

    anchor := <-anchorChan
    return anchor, err
}

When running the function, I obtain the following output, which shows that a match is indeed found and (presumably) pushed into anchorChan:

2016/05/22 14:04:36 Started circuit server with anchor: circuit://[::]:36195/20666/Q431cc5fe613aa04b

However, startCircuit's caller seems to hang. Here is the relevant bit of code:

rootAnchor, err := c.startCircuit(peer)
if err != nil {
    return "", err
}
log.Fatal(rootAnchor) // DEBUG

Why is startCircuit hanging indefinitely instead of returning?

T. Claverie

The problem is in fact quite simple. Hint: the following code ends in a deadlock.

package main

import "fmt"

func main() {
    var c chan string

    go func() {
        c = make(chan string)
        c <- "42"
    }()

    str := <-c
    fmt.Println(str)
}

From there, the problem is trivial. Your channel is not initialized when you start the goroutine. There is a race with the two goroutines, and apparently go cannot decide which should have the priority.

So, your answer is: call make(chan ...) before the goroutine starts, it should solve your problem. There is a perfect example of this in effective go.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why isn't my function returning the string?

From Dev

Why isn't my function returning a Float Number

From Dev

My recursive function isn't returning anything

From Dev

Why isn't this anonymous function returning?

From Dev

Why isn't my append function working?

From Dev

Why isn't ModRewrite returning my regular expression as expected?

From Dev

Why isn't Main returning?

From Dev

Why isn't phantomjs returning

From Dev

Why isn't the search function on my datatables.js working?

From Dev

Why isn't this generic function supported in my example?

From Dev

Why isn't my metaclass function getting invoked for subclasses?

From Dev

Why isn't my function referring to a correct route?

From Dev

Why isn't my removeString function removing the characters?

From Dev

Why isn't Typescript requiring my function to return a certain type?

From Dev

Why isn't this generic function supported in my example?

From Dev

Why my sort rows function isn't recurring

From Dev

Why isn't the search function on my datatables.js working?

From Dev

Why isn't my function referring to a correct route?

From Dev

Why isn't my regex returning the correct value for my text input?

From Dev

My function isn't working

From Dev

My Function isn't working?

From Dev

Why isn't this if statement returning the boolean value?

From Dev

Why isn't this form returning results?

From Dev

Strcmp() function isn't returning 0

From Dev

Why isn't my Asset Catalog returning R4 images?

From Dev

Why isn't my javascript returning the correct answer to Project Euler's Number One?

From Dev

Why isn't my Bash script returning the correct answer to this Project Euler?

From Dev

My function isn't returning what I expect using higher order functions

From Dev

My C function isn't returning the right int-to-binary value

Related Related

  1. 1

    Why isn't my function returning the string?

  2. 2

    Why isn't my function returning a Float Number

  3. 3

    My recursive function isn't returning anything

  4. 4

    Why isn't this anonymous function returning?

  5. 5

    Why isn't my append function working?

  6. 6

    Why isn't ModRewrite returning my regular expression as expected?

  7. 7

    Why isn't Main returning?

  8. 8

    Why isn't phantomjs returning

  9. 9

    Why isn't the search function on my datatables.js working?

  10. 10

    Why isn't this generic function supported in my example?

  11. 11

    Why isn't my metaclass function getting invoked for subclasses?

  12. 12

    Why isn't my function referring to a correct route?

  13. 13

    Why isn't my removeString function removing the characters?

  14. 14

    Why isn't Typescript requiring my function to return a certain type?

  15. 15

    Why isn't this generic function supported in my example?

  16. 16

    Why my sort rows function isn't recurring

  17. 17

    Why isn't the search function on my datatables.js working?

  18. 18

    Why isn't my function referring to a correct route?

  19. 19

    Why isn't my regex returning the correct value for my text input?

  20. 20

    My function isn't working

  21. 21

    My Function isn't working?

  22. 22

    Why isn't this if statement returning the boolean value?

  23. 23

    Why isn't this form returning results?

  24. 24

    Strcmp() function isn't returning 0

  25. 25

    Why isn't my Asset Catalog returning R4 images?

  26. 26

    Why isn't my javascript returning the correct answer to Project Euler's Number One?

  27. 27

    Why isn't my Bash script returning the correct answer to this Project Euler?

  28. 28

    My function isn't returning what I expect using higher order functions

  29. 29

    My C function isn't returning the right int-to-binary value

HotTag

Archive