qmuntal / stateless

Go library for creating finite state machines
BSD 2-Clause "Simplified" License
898 stars 47 forks source link

Panic on 32bit ARM processor #31

Closed james7272 closed 2 years ago

james7272 commented 2 years ago

The following Unaligned panic occurs on a 32 bit ARM processor

runtime/internal/atomic.panicUnaligned()
    /usr/lib/go/src/runtime/internal/atomic/unaligned.go:8 +0x24
runtime/internal/atomic.Load64(0xf482b4)
    /usr/lib/go/src/runtime/internal/atomic/atomic_arm.s:286 +0x14
github.com/qmuntal/stateless.(*StateMachine).Firing(...)
    /home/andy/Documents/werk/software/products/xte/xte_dev/vendor/github.com/qmuntal/stateless/statemachine.go:268

A simple workaround is to re-arrange the StateMachine struct to place the 'ops' field first where it has the correct byte alignment.

type StateMachine struct {
    ops                    uint64
    stateConfig            map[State]*stateRepresentation
    triggerConfig          map[Trigger]triggerWithParameters
    stateAccessor          func(context.Context) (State, error)
    stateMutator           func(context.Context, State) error
    unhandledTriggerAction UnhandledTriggerActionFunc
    onTransitioningEvents  onTransitionEvents
    onTransitionedEvents   onTransitionEvents
    eventQueue             *list.List
    firingMode             FiringMode
    firingMutex            sync.Mutex
}
qmuntal commented 2 years ago

This is unfortunate. Related to https://github.com/golang/go/issues/23345.

The workaround is to move ops to the beginning of the struct, as @james7272 recommended.

I'll cut a new release with this fixed soon.

james7272 commented 2 years ago

Thanks for the prompt fix, much appreciated.