Skip to content

Fix: Duplicate event on network disconnect#48800

Merged
thaJeztah merged 1 commit intomoby:masterfrom
AmirBuddy:48797-network-disconnect-double-event
Jan 20, 2025
Merged

Fix: Duplicate event on network disconnect#48800
thaJeztah merged 1 commit intomoby:masterfrom
AmirBuddy:48797-network-disconnect-double-event

Conversation

@AmirBuddy
Copy link
Contributor

@AmirBuddy AmirBuddy commented Oct 30, 2024

- What I did
I modified the DisconnectFromNetwork function in daemon/container_operations.go to remove redundant code that was causing duplicate network disconnect events.

- How I did it
By identifying and removing the part of the code that was generating an additional event when a network disconnection occurred.

- How to verify it

  1. Build the modified Docker from my branch.
  2. Run a container and disconnect it from a network.
  3. Use the docker events command to check that only one network disconnect event is generated for each disconnection.

- Description for the changelog

Fix duplicate network disconnect events

This fix resolves the issue where a network disconnect generates duplicate events.

Co-authored-by: eyeamnoob <a.afraz.1380@gmail.com>

Signed-off-by: AmirBuddy <badinlu.amirhossein@gmail.com>
Signed-off-by: eyeamnoob <a.afraz.1380@gmail.com>
@thaJeztah
Copy link
Member

Thanks!

Had a quick peek, and look like the other event may be coming from here;

func (daemon *Daemon) tryDetachContainerFromClusterNetwork(network *libnetwork.Network, ctr *container.Container) {
if !ctr.Managed && daemon.clusterProvider != nil && network.Dynamic() {
if err := daemon.clusterProvider.DetachNetwork(network.Name(), ctr.ID); err != nil {
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
if err := daemon.clusterProvider.DetachNetwork(network.ID(), ctr.ID); err != nil {
log.G(context.TODO()).WithError(err).Warn("error detaching from network")
}
}
}
daemon.LogNetworkEventWithAttributes(network, events.ActionDisconnect, map[string]string{
"container": ctr.ID,
})
}

As the other one is related to cluster (swarm) networks, we may have to double check if there's a codepath where we would now miss the event (to be sure).

@AmirBuddy
Copy link
Contributor Author

Thank you for the insight!

I reviewed the tryDetachContainerFromClusterNetwork function as suggested and tested both non-Swarm and Swarm network disconnections:

  1. Non-Swarm Mode: The modified version resolves the issue of duplicate network disconnect events. Only one event is now generated, as expected.

  2. Swarm Mode: I also verified that both the original and modified versions work as intended, with no redundant network disconnect events triggered in Swarm mode. I think it’s safe to say Swarm mode is unchanged, which is what we wanted.

From these tests, it looks like the changes effectively remove the duplicate event issue in non-Swarm networks without impacting Swarm functionality.

Please let me know if there’s anything else I may have missed that I should check.

Copy link
Member

@akerouanton akerouanton left a comment

Choose a reason for hiding this comment

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

I think we should move the event sent by tryDetachContainerFromClusterNetwork up by one level (ie. into disconnectFromNetwork and releaseNetwork). But let's keep this for a follow-up pr. Thanks!

@thaJeztah thaJeztah added this to the 28.0.0 milestone Oct 31, 2024
@thaJeztah
Copy link
Member

Don't merge yet; still want to have a peek as well; at least, want to look at the tryDetachContainerFromClusterNetwork again, as I notice it's firing an event regardless if there actually was a network attachment (if not, it logs an error, and falls through). In deed wondering if a different spot for the event would work.

@AmirBuddy AmirBuddy changed the title Fix: Duplicate event on network disconnect #48797 Fix: Duplicate event on network disconnect Nov 1, 2024
@AmirBuddy
Copy link
Contributor Author

Thanks, @thaJeztah! I see the concern with tryDetachContainerFromClusterNetwork firing events unnecessarily. Moving the event trigger up a level, as @akerouanton suggested, would better separate detachment and logging, similar to how connectToNetwork handles it. This can be addressed in a follow-up PR.

@vvoland vvoland requested a review from thaJeztah December 19, 2024 12:51
@vvoland vvoland requested a review from robmry December 19, 2024 13:16
Copy link
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

If DisconnectFromNetwork fails to find the network and force, or the container's not running ...

if !ctr.Running || (err != nil && force) {
if ctr.RemovalInProgress || ctr.Dead {
return errRemovalContainer(ctr.ID)
}
// In case networkName is resolved we will use n.Name()
// this will cover the case where network id is passed.
if n != nil {
networkName = n.Name()
}
if _, ok := ctr.NetworkSettings.Networks[networkName]; !ok {
return fmt.Errorf("container %s is not connected to the network %s", ctr.ID, networkName)
}
delete(ctr.NetworkSettings.Networks, networkName)

disconnectFromNetwork -> tryDetachContainerFromClusterNetwork isn't called, so it won't send an event.

But, at the moment, the event will still be sent by DisconnectFromNetwork.

Sending an event for removal of a stopped container, or from a non-existent network, doesn't sound right. So, I think the change is good ... but, it is an extra change.

@robmry
Copy link
Contributor

robmry commented Dec 19, 2024

If DisconnectFromNetwork fails to find the network and force, or the container's not running ...

Sorry, that wasn't quite right - the event will only be sent for disconnect from a network that does exist (for a stopped container).

@thompson-shaun
Copy link
Contributor

@thaJeztah - final review :D

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

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

LGTM

sorry, this one dropped of my radar, but looks like there's been enough eyes on this to make sure it's correct.

Thank you so much for contributing!

@thaJeztah thaJeztah merged commit 2c3c0c7 into moby:master Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate Events for Network Disconnect in Docker Events Command

6 participants