Memory leak: unsubscribe leaves subscribers behind after name reuse
Problem
Repeated subscribe commands with the same name cause a memory leak for the lifetime of the client connection: unsubscribe removes only the newest subscriber. Further unsubscribe calls report deleted: false, while the older internal subscribers remain allocated.
I reproduced this with Watchman 4.9.0 (4.9.0-9+b1, Debian, Linux). A long-running Django development server accumulated 165,271 internal subscribers for 160 distinct names, with roughly 1,033 copies per name. Watchman's resident memory reached 1.04 GiB.
Reproduction
Run this Python script with pywatchman installed and an existing project directory as its argument. The query matches no files; it tests whether all subscribers can be removed with unsubscribe.
import sys
import pywatchman
client = pywatchman.client(timeout=10)
root = client.query("watch-project", sys.argv[1])["watch"]
name = "subscription-replacement-probe"
query = {
"expression": ["false"],
"fields": ["name"],
"since": client.query("clock", root)["clock"],
}
def subscriber_count():
result = client.query("debug-get-subscriptions", root)
return sum(item["info"] == name for item in result["subscribers"])
try:
print("Before subscribe:", subscriber_count())
for _ in range(3):
client.query("subscribe", root, name, query)
print("After three subscribes:", subscriber_count())
for attempt in range(1, 4):
result = client.query("unsubscribe", root, name)
print("Unsubscribe", attempt, "deleted:", result["deleted"], "remaining:", subscriber_count())
finally:
client.close()Observed output on 4.9.0:
Before subscribe: 0
After three subscribes: 3
Unsubscribe 1 deleted: True remaining: 2
Unsubscribe 2 deleted: False remaining: 2
Unsubscribe 3 deleted: False remaining: 2Inspecting the root from a separate client after the original client disconnects shows zero subscribers with that name.
The two remaining subscribers cannot be removed with unsubscribe: Watchman reports that no subscription exists under that name. They are released only when the client disconnects. This demonstrates retention even without assuming that reusing a name should replace the previous subscription.
Expected: accepted subscription requests should not leave subscribers that unsubscribe cannot remove. Rejecting duplicate names before allocating a new subscriber would also prevent this accumulation.
Explicitly unsubscribing before each subscribe keeps the count at one after ten refreshes, and at zero after the final unsubscribe.
Source inspection
In cmd_subscribe, each new subscription is inserted into client->unilateralSub, keyed by the subscription object. client->subscriptions[sub->name] = sub replaces the entry keyed by name, but does not remove the previous object from unilateralSub.
A 2022 maintainer commit states that subscription management assumes names are unique within a connection. It added a duplicate-name warning and the optional enforce_unique_subscription_names error. This report concerns the retained subscribers when duplicate-name requests are accepted, rather than a promise of subscription replacement.
Current upstream still defaults enforcement to disabled, and the retention path still appears present. This is based on source inspection; I have only reproduced the behavior at runtime on 4.9.0.
Django's Watchman reloader resends subscriptions after processing requests to include newly imported modules, without first unsubscribing. This repeatedly exercises the duplicate-name path on a persistent connection.
Source: facebook/watchman