2026-02-04 11:06:13 +00:00
|
|
|
package bus
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
"errors"
|
2026-03-17 16:12:12 +00:00
|
|
|
"sync"
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
"sync/atomic"
|
2026-02-23 13:34:37 +00:00
|
|
|
|
|
|
|
|
"github.com/sipeed/picoclaw/pkg/logger"
|
2026-02-04 11:06:13 +00:00
|
|
|
)
|
|
|
|
|
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
// ErrBusClosed is returned when publishing to a closed MessageBus.
|
|
|
|
|
var ErrBusClosed = errors.New("message bus closed")
|
|
|
|
|
|
2026-02-26 14:46:57 +00:00
|
|
|
const defaultBusBufferSize = 64
|
2026-02-24 14:30:22 +00:00
|
|
|
|
2026-02-04 11:06:13 +00:00
|
|
|
type MessageBus struct {
|
2026-02-22 19:10:57 +00:00
|
|
|
inbound chan InboundMessage
|
|
|
|
|
outbound chan OutboundMessage
|
|
|
|
|
outboundMedia chan OutboundMediaMessage
|
2026-03-17 16:12:12 +00:00
|
|
|
|
|
|
|
|
closeOnce sync.Once
|
|
|
|
|
done chan struct{}
|
|
|
|
|
closed atomic.Bool
|
|
|
|
|
wg sync.WaitGroup
|
2026-02-04 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewMessageBus() *MessageBus {
|
|
|
|
|
return &MessageBus{
|
2026-02-24 14:30:22 +00:00
|
|
|
inbound: make(chan InboundMessage, defaultBusBufferSize),
|
|
|
|
|
outbound: make(chan OutboundMessage, defaultBusBufferSize),
|
|
|
|
|
outboundMedia: make(chan OutboundMediaMessage, defaultBusBufferSize),
|
2026-02-22 19:10:57 +00:00
|
|
|
done: make(chan struct{}),
|
2026-02-04 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:12:12 +00:00
|
|
|
func publish[T any](ctx context.Context, mb *MessageBus, ch chan T, msg T) error {
|
|
|
|
|
// check bus closed before acquiring wg, to avoid unnecessary wg.Add and potential deadlock
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
if mb.closed.Load() {
|
|
|
|
|
return ErrBusClosed
|
|
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
|
|
|
|
|
// check again,before sending message, to avoid sending to closed channel
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
case <-mb.done:
|
|
|
|
|
return ErrBusClosed
|
2026-03-17 16:12:12 +00:00
|
|
|
default:
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
|
|
|
|
|
mb.wg.Add(1)
|
|
|
|
|
defer mb.wg.Done()
|
|
|
|
|
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
select {
|
2026-03-17 16:12:12 +00:00
|
|
|
case ch <- msg:
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
return nil
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
2026-03-17 16:12:12 +00:00
|
|
|
case <-mb.done:
|
|
|
|
|
return ErrBusClosed
|
2026-02-16 07:04:25 +00:00
|
|
|
}
|
2026-02-04 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:12:12 +00:00
|
|
|
func (mb *MessageBus) PublishInbound(ctx context.Context, msg InboundMessage) error {
|
|
|
|
|
return publish(ctx, mb, mb.inbound, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mb *MessageBus) InboundChan() <-chan InboundMessage {
|
|
|
|
|
return mb.inbound
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mb *MessageBus) PublishOutbound(ctx context.Context, msg OutboundMessage) error {
|
|
|
|
|
return publish(ctx, mb, mb.outbound, msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (mb *MessageBus) OutboundChan() <-chan OutboundMessage {
|
|
|
|
|
return mb.outbound
|
2026-02-04 11:06:13 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-22 19:10:57 +00:00
|
|
|
func (mb *MessageBus) PublishOutboundMedia(ctx context.Context, msg OutboundMediaMessage) error {
|
2026-03-17 16:12:12 +00:00
|
|
|
return publish(ctx, mb, mb.outboundMedia, msg)
|
2026-02-22 19:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-17 16:12:12 +00:00
|
|
|
func (mb *MessageBus) OutboundMediaChan() <-chan OutboundMediaMessage {
|
|
|
|
|
return mb.outboundMedia
|
2026-02-22 19:10:57 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-04 11:06:13 +00:00
|
|
|
func (mb *MessageBus) Close() {
|
2026-03-17 16:12:12 +00:00
|
|
|
mb.closeOnce.Do(func() {
|
|
|
|
|
// notify all blocked publishers to exit
|
refactor(bus): fix deadlock and concurrency issues in MessageBus
PublishInbound/PublishOutbound held RLock during blocking channel sends,
deadlocking against Close() which needs a write lock when the buffer is
full. ConsumeInbound/SubscribeOutbound used bare receives instead of
comma-ok, causing zero-value processing or busy loops after close.
Replace sync.RWMutex+bool with atomic.Bool+done channel so Publish
methods use a lock-free 3-way select (send / done / ctx.Done). Add
context.Context parameter to both Publish methods so callers can cancel
or timeout blocked sends. Close() now only sets the atomic flag and
closes the done channel—never closes the data channels—eliminating
send-on-closed-channel panics.
- Remove dead code: RegisterHandler, GetHandler, handlers map,
MessageHandler type (zero callers across the whole repo)
- Add ErrBusClosed sentinel error
- Update all 10 caller sites to pass context
- Add msgBus.Close() to gateway and agent shutdown flows
- Add pkg/bus/bus_test.go with 11 test cases covering basic round-trip,
context cancellation, closed-bus behavior, concurrent publish+close,
full-buffer timeout, and idempotent Close
2026-02-22 16:44:45 +00:00
|
|
|
close(mb.done)
|
2026-02-23 13:34:37 +00:00
|
|
|
|
2026-03-17 16:12:12 +00:00
|
|
|
// because every publisher will check mb.closed before acquiring wg
|
|
|
|
|
// so we can be sure that new publishers will not be added new messages after this point
|
|
|
|
|
mb.closed.Store(true)
|
|
|
|
|
|
|
|
|
|
// wait for all ongoing Publish calls to finish, ensuring all messages have been sent to channels or exited
|
|
|
|
|
mb.wg.Wait()
|
|
|
|
|
|
|
|
|
|
// close channels safely
|
|
|
|
|
close(mb.inbound)
|
|
|
|
|
close(mb.outbound)
|
|
|
|
|
close(mb.outboundMedia)
|
|
|
|
|
|
|
|
|
|
// clean up any remaining messages in channels
|
2026-02-23 13:34:37 +00:00
|
|
|
drained := 0
|
2026-03-17 16:12:12 +00:00
|
|
|
for range mb.inbound {
|
|
|
|
|
drained++
|
2026-02-23 13:34:37 +00:00
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
for range mb.outbound {
|
|
|
|
|
drained++
|
2026-02-23 13:34:37 +00:00
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
for range mb.outboundMedia {
|
|
|
|
|
drained++
|
2026-02-23 13:34:37 +00:00
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
|
2026-02-23 13:34:37 +00:00
|
|
|
if drained > 0 {
|
|
|
|
|
logger.DebugCF("bus", "Drained buffered messages during close", map[string]any{
|
|
|
|
|
"count": drained,
|
|
|
|
|
})
|
|
|
|
|
}
|
2026-03-17 16:12:12 +00:00
|
|
|
})
|
2026-02-04 11:06:13 +00:00
|
|
|
}
|