Skip to content

Conversation

Copy link

Copilot AI commented Jun 11, 2025

This PR fixes a duplicate ATTACH message issue that occurred when channel.attach() was called before the connection was established.

Problem

When a channel was attached before the connection was fully established, the following sequence would result in duplicate ATTACH messages:

  1. User calls channel.attach() while connection is still connecting
  2. RTL4i fix correctly prevents immediate ATTACH message and sets channel to attaching state
  3. Connection becomes connected
  4. ConnectionManager calls setConnected() on all channels
  5. setConnected() calls attach(true, null) on all "reattachable" channels (including those in attaching state)
  6. This sends a duplicate ATTACH message for channels already in the attach process

Root Cause

The setConnected() method was using state.isReattachable() which returns true for channels in attaching, attached, or suspended states. However, channels already in attaching state should continue their existing attach process rather than starting a new one.

Solution

Modified setConnected() to only re-attach channels that are in attached or suspended states, excluding channels already in attaching state:

public void setConnected() {
    // RTN15c6: Re-attach channels in ATTACHED state
    // RTN15c7: Re-attach channels in SUSPENDED state  
    // Channels in ATTACHING state should continue their existing attach process
    if (state == ChannelState.attached || state == ChannelState.suspended) {
        attach(true,null); // RTN15c6, RTN15c7
    }
}

This ensures compliance with Ably specifications:

  • RTN15c6: Re-attach channels in ATTACHED state on reconnection
  • RTN15c7: Re-attach channels in SUSPENDED state on reconnection
  • RTL4i: Prevent ATTACH during connecting/disconnected states (already implemented)

Testing

Added test attach_before_connect_no_duplicate_attach() that:

  • Monitors protocol messages using RawProtocolMonitor
  • Calls channel.attach() before connection is established
  • Verifies exactly one ATTACH message is sent
  • Confirms channel reaches attached state successfully

Changes

  • ChannelBase.java: Fixed setConnected() method to prevent duplicate ATTACH for channels in attaching state
  • RealtimeChannelTest.java: Added comprehensive test to verify the fix
  • Removed TODO comment that referenced this issue

This fix works in conjunction with the existing duplicate ATTACH prevention in ConnectionManager.sendQueuedMessages() (from PR #1052) to fully resolve the duplicate ATTACH issue.

Fixes #1018.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dl.google.com
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED -Xmx1536M -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en -Duser.variant -cp /home/REDACTED/.gradle/wrapper/dists/gradle-8.7-all/aan3ydargesu18aqyqjwhr3pc/gradle-8.7/lib/gradle-launcher-8.7.jar -javaagent:/home/REDACTED/.gradle/wrapper/dists/gradle-8.7-all/aan3ydargesu18aqyqjwhr3pc/gradle-8.7/lib/agents/gradle-instrumentation-agent-8.7.jar org.gradle.launcher.daemon.bootstrap.GradleDaemon 8.7 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@jamienewcomb jamienewcomb deleted the copilot/fix-1018 branch June 11, 2025 10:41
Copilot AI restored the copilot/fix-1018 branch June 11, 2025 10:48
Copilot AI changed the title [WIP] Avoid duplicate ATTACH sent Fix duplicate ATTACH messages when attaching before connection is established Jun 11, 2025
Copilot AI requested a review from jamienewcomb June 11, 2025 10:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Avoid duplicate ATTACH sent

2 participants