Skip to content

Block Sends when Resend Request is active #715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions in_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ func (state inSession) resendMessages(session *session, beginSeqNo, endSeqNo int
return state.generateSequenceReset(session, beginSeqNo, endSeqNo+1, inReplyTo)
}

// resendMutex must always be locked before sendMutex to prevent a potential deadlock
// sendMutex is locked below in session.EnqueueBytesAndSend()
session.resendMutex.Lock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please place a comment indicating that this lock must always be locked first before the sendMutex, otherwise there is potential for a deadlock, and that mutex is locked within the session.EnqueueBytesAndSend method below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer session.resendMutex.Unlock()

seqNum := beginSeqNo
nextSeqNum := seqNum
msg := NewMessage()
Expand Down
20 changes: 20 additions & 0 deletions in_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"

"github.com/quickfixgo/quickfix/internal"
Expand Down Expand Up @@ -373,6 +374,25 @@ func (s *InSessionTestSuite) TestFIXMsgInResendRequestDoNotSendApp() {
s.State(inSession{})
}

func (s *InSessionTestSuite) TestFIXMsgInResendRequestBlocksSend() {
s.MockApp.On("ToApp").Return(nil)
s.Require().Nil(s.session.send(s.NewOrderSingle()))
s.LastToAppMessageSent()
s.MockApp.AssertNumberOfCalls(s.T(), "ToApp", 1)
s.NextSenderMsgSeqNum(2)

s.MockStore.On("IterateMessages", mock.Anything, mock.Anything, mock.AnythingOfType("func([]byte) error")).
Run(func(_ mock.Arguments) {
s.Require().Nil(s.session.send(s.NewOrderSingle()))
}).
Return(nil)

s.MockApp.On("FromAdmin").Return(nil)
s.fixMsgIn(s.session, s.ResendRequest(1))

s.NextSenderMsgSeqNum(2)
}

func (s *InSessionTestSuite) TestFIXMsgInTargetTooLow() {
s.IncrNextTargetMsgSeqNum()

Expand Down
7 changes: 7 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type session struct {

// Mutex for access to toSend.
sendMutex sync.Mutex
// Mutex to prevent messages being sent when resendRequest is active
// Must be locked before sendMutex to prevent a potential deadlock
resendMutex sync.RWMutex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additional comment above this as well indicating the reason why this mutex exists in conjunction with the sendMutex and in which order they must be locked to avoid deadlocks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


sessionEvent chan internal.Event
messageEvent chan bool
Expand Down Expand Up @@ -302,6 +305,10 @@ func (s *session) sendInReplyTo(msg *Message, inReplyTo *Message) error {
return s.queueForSend(msg)
}

// resendMutex must always be locked before sendMutex to prevent a potential deadlock
s.resendMutex.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mirror other comment here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer s.resendMutex.RUnlock()

s.sendMutex.Lock()
defer s.sendMutex.Unlock()

Expand Down
Loading