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

Skip to content

Prevent non-blocking select from making function appear blocking. #1108

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

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ func (fc *funcContext) translateStmt(stmt ast.Stmt, label *types.Label) {
Fun: fc.newIdent("$select", types.NewSignature(nil, types.NewTuple(types.NewVar(0, nil, "", types.NewInterface(nil, nil))), types.NewTuple(types.NewVar(0, nil, "", types.Typ[types.Int])), false)),
Args: []ast.Expr{fc.newIdent(fmt.Sprintf("[%s]", strings.Join(channels, ", ")), types.NewInterface(nil, nil))},
}, types.Typ[types.Int])
fc.Blocking[selectCall] = !hasDefault
if !hasDefault {
fc.Blocking[selectCall] = true
}
fc.Printf("%s = %s;", selectionVar, fc.translateExpr(selectCall))

if len(caseClauses) != 0 {
Expand Down
23 changes: 23 additions & 0 deletions tests/goroutine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"runtime"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -289,3 +290,25 @@ func TestGoroutineJsObject(t *testing.T) {
runtime.Gosched()
}
}

func issue1106() {
select {
default:
}
}

func TestIssue1106(t *testing.T) {
// https://github.com/gopherjs/gopherjs/issues/1106#issuecomment-1046323374
var done int32 = 0
go func() {
f := issue1106
f()
atomic.AddInt32(&done, 1)
}()

// Will get stuck here if #1106 is not fixed.
for !atomic.CompareAndSwapInt32(&done, 1, 1) {
// Maintain one active goroutine to prevent Node from exiting.
runtime.Gosched()
}
}