Skip to content

Commit

Permalink
fix(runtime:funcs): deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Sep 23, 2023
1 parent 0869079 commit 0b9658e
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 23 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ run:
linters:
enable-all: true
disable:
- depguard # enforces to manually configure imports white-list
- godox # todo/fixme
- godot # dots at the end of comments
- gomnd # magic number
Expand Down
2 changes: 1 addition & 1 deletion cmd/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
}

intr := interpreter.MustNew(
parser.New(),
parser.New(false),
irgen.New(),
interpreter.MustNewTransformer(),
runTime,
Expand Down
1 change: 1 addition & 0 deletions internal/interpreter/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (t transformer) Transform(ctx context.Context, irprog *ir.Program) (runtime
func (t transformer) msg(msg *ir.Msg) (runtime.Msg, error) {
var rMsg runtime.Msg

//nolint:nosnakecase
switch msg.Type {
case ir.MsgType_MSG_TYPE_BOOL:
rMsg = runtime.NewBoolMsg(msg.Bool)
Expand Down
22 changes: 15 additions & 7 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ type treeShapeListener struct {
file src.Package
}

type Parser struct{}
type Parser struct {
isDebug bool
}

func (p Parser) ParseFiles(ctx context.Context, files map[string][]byte) (map[string]src.Package, error) {
result := make(map[string]src.Package, len(files))
Expand All @@ -43,17 +45,23 @@ func (p Parser) ParseFile(ctx context.Context, bb []byte) (src.Package, error) {
input := antlr.NewInputStream(string(bb))
lexer := generated.NewnevaLexer(input)
stream := antlr.NewCommonTokenStream(lexer, 0)
prsr := generated.NewnevaParser(stream)
prsr.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
prsr.BuildParseTrees = true
tree := prsr.Prog()

parse := generated.NewnevaParser(stream)
if p.isDebug {
parse.AddErrorListener(antlr.NewDiagnosticErrorListener(true))
}
parse.BuildParseTrees = true

tree := parse.Prog()
listener := &treeShapeListener{}

antlr.ParseTreeWalkerDefault.Walk(listener, tree)

return listener.file, nil
}

func New() Parser {
return Parser{}
func New(isDebug bool) Parser {
return Parser{
isDebug: isDebug,
}
}
6 changes: 3 additions & 3 deletions internal/runtime/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ func (c DefaultConnector) distribute(
type DefaultInterceptor struct{}

func (i DefaultInterceptor) AfterSending(from SenderConnectionSideMeta, msg Msg) Msg {
fmt.Printf("after sending %v -> %v\n", from, msg)
// fmt.Printf("after sending: %v -> %v\n", from, msg)
return msg
}

func (i DefaultInterceptor) BeforeReceiving(from SenderConnectionSideMeta, to ReceiverConnectionSideMeta, msg Msg) Msg {
fmt.Printf("before receiving %v <- %v <- %v\n", to, msg, from)
// fmt.Printf("before receiving: %v -> %v -> %v\n", from, msg, to)
return msg
}

func (i DefaultInterceptor) AfterReceiving(from SenderConnectionSideMeta, to ReceiverConnectionSideMeta, msg Msg) {
fmt.Printf("after receiving %v -> %v -> %v\n", from, msg, to)
// fmt.Printf("after receiving: %v -> %v -> %v\n", from, msg, to)
}
33 changes: 24 additions & 9 deletions internal/runtime/funcs/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ func Print(ctx context.Context, io runtime.FuncIO) (func(), error) {
case <-ctx.Done():
return
case v := <-vin:
fmt.Println(v.String())
vout <- v
select {
case <-ctx.Done():
return
default:
fmt.Println(v.String())
select {
case <-ctx.Done():
return
case vout <- v:
}
}
}
}
}, nil
Expand All @@ -47,12 +56,18 @@ func Lock(ctx context.Context, io runtime.FuncIO) (func(), error) {
for {
select {
case <-ctx.Done():
fmt.Println(ctx.Err())
return
default:
<-sig
v := <-vin
vout <- v
case <-sig:
select {
case <-ctx.Done():
return
case v := <-vin:
select {
case <-ctx.Done():
return
case vout <- v:
}
}
}
}
}, nil
Expand All @@ -63,6 +78,7 @@ func Const(ctx context.Context, io runtime.FuncIO) (func(), error) {
if msg == nil {
return nil, errors.New("ctx msg not found")
}

v, ok := msg.(runtime.Msg)
if !ok {
return nil, errors.New("ctx value is not runtime message")
Expand All @@ -78,8 +94,7 @@ func Const(ctx context.Context, io runtime.FuncIO) (func(), error) {
select {
case <-ctx.Done():
return
default:
vout <- v
case vout <- v:
}
}
}, nil
Expand Down
2 changes: 1 addition & 1 deletion internal/runtime/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type StrMsg struct {
}

func (msg StrMsg) Str() string { return msg.v }
func (msg StrMsg) String() string { return strconv.Quote(msg.v) }
func (msg StrMsg) String() string { return msg.v }

func NewStrMsg(s string) StrMsg {
return StrMsg{
Expand Down
4 changes: 4 additions & 0 deletions internal/runtime/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type SenderConnectionSideMeta struct {
PortAddr PortAddr
}

func (c SenderConnectionSideMeta) String() string {
return c.PortAddr.String()
}

type ReceiverConnectionSideMeta struct {
PortAddr PortAddr
Selectors []string
Expand Down
2 changes: 0 additions & 2 deletions internal/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ func (r Runtime) Run(ctx context.Context, prog Program) (code int, err error) {

var exitCode int64
go func() {
msg := <-exit
fmt.Println(msg)
exitCode = (<-exit).Int()
cancel()
}()
Expand Down

0 comments on commit 0b9658e

Please sign in to comment.