Skip to content

Fix panic when response failed by timeout #396

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
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
17 changes: 17 additions & 0 deletions pkg/stream/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,21 @@ var _ = Describe("Streaming testEnvironment", func() {
Expect(res).To(BeNil())
})

It("Client.handleGenericResponse handles timeout and missing response gracefully", func() {
cli := newClient("connName", nil, nil, nil, defaultSocketCallTimeout)

// Simulate timeout: create a response and remove it immediately
res := cli.coordinator.NewResponse(commandDeclarePublisher, "Simulated Test")
err := cli.coordinator.RemoveResponseById(res.correlationid)
Expect(err).To(BeNil())

// Simulate receiving a response for the removed correlation ID
readerProtocol := &ReaderProtocol{
CorrelationId: uint32(res.correlationid),
ResponseCode: responseCodeStreamNotAvailable,
}
cli.handleGenericResponse(readerProtocol, bufio.NewReader(bytes.NewBuffer([]byte{})))

})

})
2 changes: 1 addition & 1 deletion pkg/stream/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (coordinator *Coordinator) GetResponseById(id uint32) (*Response, error) {
if err != nil {
return nil, err
}
return v.(*Response), err
return v.(*Response), nil
}

func (coordinator *Coordinator) ConsumersCount() int {
Expand Down
68 changes: 53 additions & 15 deletions pkg/stream/server_frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (c *Client) handleResponse() {
}
}

func (c *Client) handleSaslHandshakeResponse(streamingRes *ReaderProtocol, r *bufio.Reader) interface{} {
func (c *Client) handleSaslHandshakeResponse(streamingRes *ReaderProtocol, r *bufio.Reader) {
streamingRes.CorrelationId, _ = readUInt(r)
streamingRes.ResponseCode = uShortExtractResponseCode(readUShort(r))
mechanismsCount, _ := readUInt(r)
Expand All @@ -158,12 +158,11 @@ func (c *Client) handleSaslHandshakeResponse(streamingRes *ReaderProtocol, r *bu

res, err := c.coordinator.GetResponseById(streamingRes.CorrelationId)
if err != nil {
// TODO handle response
return err
logErrorCommand(err, "handleSaslHandshakeResponse")
return
}
res.data <- mechanisms

return mechanisms
res.data <- mechanisms
}

func (c *Client) handlePeerProperties(readProtocol *ReaderProtocol, r *bufio.Reader) {
Expand All @@ -178,7 +177,11 @@ func (c *Client) handlePeerProperties(readProtocol *ReaderProtocol, r *bufio.Rea
serverProperties[key] = value
}
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "handlePeerProperties")
if err != nil {
logErrorCommand(err, "handlePeerProperties")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- serverProperties

Expand Down Expand Up @@ -210,7 +213,11 @@ func (c *Client) handleGenericResponse(readProtocol *ReaderProtocol, r *bufio.Re
readProtocol.CorrelationId, _ = readUInt(r)
readProtocol.ResponseCode = uShortExtractResponseCode(readUShort(r))
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "handleGenericResponse")
if err != nil {
logErrorCommand(err, "handleGenericResponse")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
}

Expand All @@ -237,7 +244,11 @@ func (c *Client) commandOpen(readProtocol *ReaderProtocol, r *bufio.Reader) {
}

res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "commandOpen")
if err != nil {
logErrorCommand(err, "commandOpen")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- clientProperties

Expand Down Expand Up @@ -277,7 +288,11 @@ func (c *Client) queryPublisherSequenceFrameHandler(readProtocol *ReaderProtocol
readProtocol.ResponseCode = uShortExtractResponseCode(readUShort(r))
sequence := readInt64(r)
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "queryPublisherSequenceFrameHandler")
if err != nil {
logErrorCommand(err, "queryPublisherSequenceFrameHandler")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- sequence
}
Expand Down Expand Up @@ -458,7 +473,11 @@ func (c *Client) queryOffsetFrameHandler(readProtocol *ReaderProtocol,
c.handleGenericResponse(readProtocol, r)
offset := readInt64(r)
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "queryOffsetFrameHandler")
if err != nil {
logErrorCommand(err, "queryOffsetFrameHandler")
return
}

res.data <- offset
}

Expand Down Expand Up @@ -516,7 +535,11 @@ func (c *Client) streamStatusFrameHandler(readProtocol *ReaderProtocol,
streamStatus[key] = value
}
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "streamStatusFrameHandler")
if err != nil {
logErrorCommand(err, "streamStatusFrameHandler")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- streamStatus

Expand Down Expand Up @@ -553,7 +576,10 @@ func (c *Client) metadataFrameHandler(readProtocol *ReaderProtocol,
}

res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "metadataFrameHandler")
if err != nil {
logErrorCommand(err, "metadataFrameHandler")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- streamsMetadata
Expand Down Expand Up @@ -612,7 +638,11 @@ func (c *Client) handleQueryPartitions(readProtocol *ReaderProtocol, r *bufio.Re
partitions = append(partitions, partition)
}
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "handleQueryPartitions")
if err != nil {
logErrorCommand(err, "handleQueryPartitions")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- partitions
}
Expand All @@ -629,7 +659,11 @@ func (c *Client) handleQueryRoute(readProtocol *ReaderProtocol, r *bufio.Reader)
}

res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "handleQueryRoute")
if err != nil {
logErrorCommand(err, "handleQueryRoute")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- routes
}
Expand All @@ -646,7 +680,11 @@ func (c *Client) handleExchangeVersionResponse(readProtocol *ReaderProtocol, r *
commands = append(commands, newCommandVersionResponse(minVersion, maxVersion, commandKey))
}
res, err := c.coordinator.GetResponseById(readProtocol.CorrelationId)
logErrorCommand(err, "handleExchangeVersionResponse")
if err != nil {
logErrorCommand(err, "handleExchangeVersionResponse")
return
}

res.code <- Code{id: readProtocol.ResponseCode}
res.data <- commands
}
Expand Down