github-vet / github-vet/rangeloop-pointer-findings

gebv/ffsm: fsm.go; 88 LoC

Open
#10,669 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [gebv/ffsm](https://www.github.com/gebv/ffsm) at [fsm.go](https://github.com/gebv/ffsm/blob/96f90d78dea231aefa4d17d47999f1ec3ffd4b24/fsm.go#L149-L236)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable m used in defer or goroutine at line 192

[Click here to see the code in its original context.](https://github.com/gebv/ffsm/blob/96f90d78dea231aefa4d17d47999f1ec3ffd4b24/fsm.go#L149-L236)

Click here to show the 88 line(s) of Go which triggered the analyzer.

```go
for m := range e.toDispatch {
atomic.AddUint64(&e.numProcessed, 1)
dispatchStart = time.Now()
err = nil
current = e.State()

if current == UnknownState {
if m.done != nil {
m.done <- ErrNotInitalState
continue
}
}

actions = e.wf.Get(current, m.next)
if actions == nil {
m.done <- ErrNotRegTransition
continue
}

if m.ctx.Err() != nil {
m.done <- m.ctx.Err()
continue
}

nextCtx, cancel := context.WithCancel(hydrateContextForAction(m.ctx, current, m.next))
defer cancel()

for _i, actionFn := range actions {
actionStart = time.Now()
actionRes = make(chan resultOfActionTransition, 1)

// For simple FSM, without transition handlers
if actionFn == nil {
continue
}

go func(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
actionRes <- resultOfActionTransition{
err: dispatcherError{
Recover: r,
SrcState: current,
DstState: m.next,
IndexAction: _i,
DebugStack: string(debug.Stack()),
},
}
return
}
}()

ctx, err := actionFn(nextCtx)
actionRes <- resultOfActionTransition{
err: err,
ctx: ctx,
}
}(nextCtx)

// waiting done action
select {
case done := <-actionRes:
nextCtx = done.ctx
err = done.err
}

actName := fmt.Sprintf("%q -> %q #%d", current, m.next, _i)
e.mActionDuration.WithLabelValues(actName).Observe(float64(time.Since(actionStart).Nanoseconds() / int64(time.Millisecond)))
e.mActionRequest.WithLabelValues(actName).Inc()

if err != nil {
// exit transition, because there was an error on one
// of the handlers of transition
break
}

// forend actions
}

if err == nil {
e.SetState(m.next)
}
m.done <- err

e.mActionDuration.WithLabelValues(e.name).Observe(float64(time.Since(dispatchStart).Nanoseconds() / int64(time.Millisecond)))
e.mTotalRequest.WithLabelValues(e.name).Inc()

} // forend dispatch

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 96f90d78dea231aefa4d17d47999f1ec3ffd4b24

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.