Skip to content

Conversation

@chaofengliu-okg
Copy link
Contributor

@chaofengliu-okg chaofengliu-okg commented Sep 10, 2025

scripts: add Watch.Watcher#cancel to cancel a single watcher

Motivation
Currently, closing a watcher is only possible via Watcher#close, which merely sends an HTTP/2 END_STREAM frame. However, etcd does not terminate the underlying goroutines and watcher stream in response, which can lead to memory leaks.

Solution
By adding a cancel function that directly sends an HTTP/2 RST_STREAM frame, etcd can properly shut down the goroutines and the watcher stream.

Reproduction

for () {
    Watch.Watcher watcher = watchClient.watch(
        key,
        (WatchResponse resp) -> {
            System.out.println("Received event: " + resp);
        },
        (Throwable t) -> {
            System.out.println("Error: " + t);
        },
        () -> {
            System.out.println("Completed");
        }
    );
    // watcher.cancel();
    watcher.close();
    System.in.read();
}

After Change
After adding the cancel API, replacing watcher.close() with watcher.cancel() allows the etcd server to properly release the stream and goroutines.

Fixes #1510
@fanminshi @lburgazzoli @vorburger

@k8s-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: chaofengliu-okg
Once this PR has been reviewed and has the lgtm label, please assign vorburger for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@chaofengliu-okg chaofengliu-okg force-pushed the feat/add-cancel-for-watcher branch from 7b2f9c3 to 6ea9fe4 Compare September 10, 2025 08:53
@chaofengliu-okg chaofengliu-okg force-pushed the feat/add-cancel-for-watcher branch from 11ed645 to 738b55c Compare September 10, 2025 10:31
@lburgazzoli
Copy link
Collaborator

should this be part of close ?

@chaofengliu-okg
Copy link
Contributor Author

If we alter the close implementation, could this break existing user dependencies on the current close behavior?

@lburgazzoli
Copy link
Collaborator

I think this is a leftover from the original design where we were supposed to re-use the same stream across multiple watcher, but this is not the case as today, so I'd for merging the functions.

@chaofengliu-okg chaofengliu-okg force-pushed the feat/add-cancel-for-watcher branch from ff4c9a6 to aa578a9 Compare October 23, 2025 09:21
@chaofengliu-okg
Copy link
Contributor Author

I've changed the modification status from 'cancel' to 'close'. Please review again.

@lburgazzoli
Copy link
Collaborator

@chaofengliu-okg is there any way to add a test for this case ?

@chaofengliu-okg
Copy link
Contributor Author

@lburgazzoli I have added the unit tests.

@lburgazzoli lburgazzoli merged commit 90301a4 into etcd-io:main Oct 24, 2025
9 checks passed
@chaofengliu-okg
Copy link
Contributor Author

chaofengliu-okg commented Oct 27, 2025

​​Thank you for merging the PR. I would like to ask about the schedule for the next release.​
@lburgazzoli

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

ServerWatchStream can't be closed when client closed watcher

3 participants