Skip to content

Commit

Permalink
fix: ring buffer deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
IronCore864 committed Oct 15, 2024
1 parent 9cb6317 commit c68f72c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 2 additions & 0 deletions internals/servicelog/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ func (it *iterator) Close() error {
if it.rb == nil {
return nil
}
it.rb.iteratorMutex.Lock()
defer it.rb.iteratorMutex.Unlock()
it.rb.removeIterator(it)
close(it.nextChan)
it.rb = nil
Expand Down
6 changes: 2 additions & 4 deletions internals/servicelog/ringbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ func (rb *RingBuffer) Write(p []byte) (written int, err error) {
}
defer func() {
if written > 0 {
rb.iteratorMutex.RLock()
defer rb.iteratorMutex.RUnlock()
rb.signalIterators()
}
}()
Expand Down Expand Up @@ -336,8 +338,6 @@ func (rb *RingBuffer) discard(n int) error {
}

func (rb *RingBuffer) signalIterators() {
rb.iteratorMutex.RLock()
defer rb.iteratorMutex.RUnlock()
for _, iter := range rb.iteratorList {
select {
case iter.nextChan <- true:
Expand Down Expand Up @@ -365,8 +365,6 @@ func (rb *RingBuffer) releaseIterators() {
}

func (rb *RingBuffer) removeIterator(iter *iterator) {
rb.iteratorMutex.Lock()
defer rb.iteratorMutex.Unlock()
for i, storedIter := range rb.iteratorList {
if iter != storedIter {
continue
Expand Down

0 comments on commit c68f72c

Please sign in to comment.