Thanks to visit codestin.com
Credit goes to github.com

Skip to content

os.Read() appears to block other goroutines from executing #529

Closed
@flimzy

Description

@flimzy

In some situation I have yet to specifically identify, GopherJS hangs. I'm creating this issue to track my debugging progress.

I found, while working on #528, that the first example for the upstream 'html/template' package causes a freeze. I have narrowed this down to a stand-alone reproduction case below. But I still need to work on narrowing it down further.

When I execute the following code in Go:

$ go run jstest.go -test.v
=== RUN   Example
--- PASS: Example (0.00s)
PASS

Or in GopherJS:

$ gopherjs run jstest.go -- -test.v
=== RUN   Example

(and never exits)

package main

import (
    "html/template"
    "log"
    "os"

    "regexp"
    "testing"
)

var tests = []testing.InternalTest{}
var benchmarks = []testing.InternalBenchmark{}

var examples = []testing.InternalExample{
    {"Example", Example, "<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t\t<title>My page</title>\n\t</head>\n\t<body>\n\t\t<div>My photos</div><div>My blog</div>\n\t</body>\n</html>\n<!DOCTYPE html>\n<html>\n\t<head>\n\t\t<meta charset=\"UTF-8\">\n\t\t<title>My another page</title>\n\t</head>\n\t<body>\n\t\t<div><strong>no rows</strong></div>\n\t</body>\n</html>\n", false},
}

var matchPat string
var matchRe *regexp.Regexp

func matchString(pat, str string) (result bool, err error) {
    if matchRe == nil || matchPat != pat {
        matchPat = pat
        matchRe, err = regexp.Compile(matchPat)
        if err != nil {
            return
        }
    }
    return matchRe.MatchString(str), nil
}

func main() {
    m := testing.MainStart(matchString, tests, benchmarks, examples)

    os.Exit(m.Run())
}

func Example() {
    const tpl = `
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>{{.Title}}</title>
    </head>
    <body>
        {{range .Items}}<div>{{ . }}</div>{{else}}<div><strong>no rows</strong></div>{{end}}
    </body>
</html>`

    check := func(err error) {
        if err != nil {
            log.Fatal(err)
        }
    }
    t, err := template.New("webpage").Parse(tpl)
    check(err)

    data := struct {
        Title string
        Items []string
    }{
        Title: "My page",
        Items: []string{
            "My photos",
            "My blog",
        },
    }

    err = t.Execute(os.Stdout, data)
    check(err)

    noItems := struct {
        Title string
        Items []string
    }{
        Title: "My another page",
        Items: []string{},
    }

    err = t.Execute(os.Stdout, noItems)
    check(err)

    // Output:
    // <!DOCTYPE html>
    // <html>
    //  <head>
    //      <meta charset="UTF-8">
    //      <title>My page</title>
    //  </head>
    //  <body>
    //      <div>My photos</div><div>My blog</div>
    //  </body>
    // </html>
    // <!DOCTYPE html>
    // <html>
    //  <head>
    //      <meta charset="UTF-8">
    //      <title>My another page</title>
    //  </head>
    //  <body>
    //      <div><strong>no rows</strong></div>
    //  </body>
    // </html>
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions