Skip to content

fix: SendAsync doesn't respect context and can't timeout during reconnection - #1422

Merged
BewareMyPower merged 6 commits into
apache:masterfrom
RobertIndie:fix-send-async-cb
Sep 15, 2025
Merged

BewareMyPower merged 6 commits into
apache:masterfrom
RobertIndie:fix-send-async-cb

Conversation

@RobertIndie

@RobertIndie RobertIndie commented Sep 11, 2025 •

Copy link
Copy Markdown
Member

Fixes #1332

Motivation

The root cause of issue #1332 is that the producer's main event loop becomes blocked during the reconnection process. This prevents it from processing new send requests from the dataChan.

Consequently, messages waiting in dataChan cannot be moved to the pending message queue, causing them to bypass the SendTimeout check. Furthermore, the context passed to SendAsync is not respected when the loop is blocked.

This behavior was introduced after PR #1029, which merged reconnection logic and data handling logic into a single event loop. The root cause of the issue in #1027 is that during producer reconnection, resending messages and sending new messages occur at the same time. This causes the messages to be sent out of order, which leads to the "received an ack larger than expected" error.

Modifications

This PR refactors the producer's reconnection logic to solve the blocking issue while also preventing the original message ordering problem.

The key changes are:

  1. Separate Reconnection and Data Handling Logic: The reconnection process has been moved into its own dedicated goroutine (event loop). This ensures the main producer data loop remains unblocked and can continuously process send requests, even when a reconnection is in progress.

  2. Introduce producerConnecting State: A new state, producerConnecting, is introduced to manage the producer's behavior during reconnection. When in this state, new messages are added to the pending queue but are not immediately sent. This prevents new messages from being sent concurrently with resent messages from the previous connection, thus fixing the root cause of the out-of-order issue from Continuous reconnection failures lead to message send timeout #1027.

  3. Respect SendAsync Context: The ctx passed to SendAsync is now respected.

New Reconnection Flow

The new logic creates a clear separation of concerns between the two event loops:

Reconnection event loop Producer data event loop
Receive the connection-closed event
At this point, the connection is closed. Messages sent to a closed connection will be ignored and will be resent after reconnection.
Set the state to producerConnecting. Start reconnecting
At this point, new messages will only be pushed into the pending queue without being sent to the connection.
Connected to the broker, start resending pending messages
At this point, new messages are still prevented from being sent to the new connection until resending has finished. Otherwise, this would cause messages to be delivered out of order, which is the root cause of #1027.
Resend finishes. Set the state to producerReady
At this point, new messages will be sent to the new connection normally.

Documentation Updates

The documentation for SendAsync has been updated to clarify the distinct roles of the context and the SendTimeout configuration:

The context passed in the SendAsync call is only used for the duration of the SendAsync call itself (i.e., to control blocking when the queue is full), and not for the entire message lifetime. Once SendAsync returns successfully, the message's lifetime is controlled by the SendTimeout configuration.

Verifying this change

Added a new unit test, TestSendAsyncCouldTimeoutWhileReconnecting (based on the test from PR #1345), to verify that message timeouts and context cancellations function correctly during a producer reconnection cycle. And also other unit tests to cover changes.

Does this pull request potentially affect one of the following parts:

If yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): (yes / no)
  • The public API: (yes / no)
  • The schema: (yes / no / don't know)
  • The default values of configurations: (yes / no)
  • The wire protocol: (yes / no)

Documentation

  • Does this pull request introduce a new feature? (yes / no)
  • If yes, how is the feature documented? (not applicable / docs / GoDocs / not documented)
  • If a feature is not applicable for documentation, explain why?
  • If a feature is not documented yet in this PR, please create a followup issue for adding the documentation

@RobertIndie RobertIndie self-assigned this Sep 11, 2025
@RobertIndie
RobertIndie marked this pull request as ready for review September 11, 2025 10:17
@RobertIndie RobertIndie added this to the v0.17.0 milestone Sep 11, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes issue #1332 where SendAsync doesn't respect context and can't timeout during producer reconnection. The root cause was that the producer's main event loop became blocked during reconnection, preventing it from processing new send requests and bypassing timeout checks.

  • Separates reconnection logic into a dedicated goroutine to prevent blocking the main producer data loop
  • Introduces a new producerConnecting state to manage message flow during reconnection
  • Ensures SendAsync respects the provided context for cancellation and timeout

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
pulsar/producer.go Updates SendAsync documentation to clarify context usage vs SendTimeout
pulsar/producer_partition.go Refactors reconnection logic, adds producerConnecting state, and implements context handling
pulsar/producer_test.go Adds comprehensive tests for timeout and context cancellation scenarios
pulsar/internal/blocking_queue.go Adds Iterate method for efficient queue traversal
pulsar/internal/blocking_queue_test.go Tests for the new Iterate functionality

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread pulsar/producer_partition.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread pulsar/producer_partition.go Resolved
@RobertIndie
RobertIndie requested a review from Copilot September 11, 2025 11:35
@RobertIndie RobertIndie changed the title fix: SendAsync doesn't respect context and can't timeout during recon nection fix: SendAsync doesn't respect context and can't timeout during reconnection Sep 11, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread pulsar/producer_partition.go Resolved
Comment thread pulsar/producer_partition.go Resolved
Comment thread pulsar/producer_partition.go Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread pulsar/producer_partition.go Resolved
Comment thread pulsar/producer_test.go Resolved
Comment thread pulsar/producer_test.go Resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][Producer] The callback was not invoked during reconnecting.

3 participants