Skip to content

Commit 2fe6d08

Browse files
committed
A device counts itself online only while it says it is here
./check.sh full failed once on going_and_settled_test.dart, over the JS relay: a device that could no longer serve as a juror still counted itself online, after the device watching it had stopped. online() counts this device by its own word (not silent, able to serve), and also, by accident, from its own beat heard back: a relay sends a device what it says, as it sends everyone, and that beat stays fresh for a minute. It left the count only when its own "gone" came back too, and which of two sockets a relay hands an event to first is not fixed, so under load the watcher heard it first. A device that went quiet whose "gone" never came back counted itself for the whole minute. online() now leaves this device's own beats out of what it has heard: it is here while it says so. presence_test.dart hands a device its own beat and then makes it unable to serve, and silent: it failed before, counting itself, and passes now. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01W92ERJ2vuziEpghjrqPe5w
1 parent ee63546 commit 2fe6d08

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎packages/table/lib/src/presence_relay.dart‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,12 @@ class RelayPresence implements Presence {
245245
Set<PublicKey> online({required int withinMs}) {
246246
final now = _nowMs;
247247
return {
248+
// This device is here while it says so, and only then: a relay
249+
// sends it its own beat as it sends everyone's, and the beat it
250+
// said before it went silent or could no longer serve is fresh for
251+
// a minute after.
248252
for (final e in _seen.entries)
249-
if (now - e.value < withinMs && !_went.contains(e.key) && !_outdone(e.key)) e.key,
253+
if (e.key != signer.publicKey && now - e.value < withinMs && !_went.contains(e.key) && !_outdone(e.key)) e.key,
250254
if (!_silent && _able) signer.publicKey,
251255
};
252256
}

‎packages/table/test/presence_test.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,20 @@ void main() {
5959
presence.receive(here(them, RelayPresence.seated, after: 1));
6060
expect(presence.people(withinMs: 15000), {me.publicKey, them.publicKey});
6161
});
62+
63+
test('count this device while it says it is here, not by its own beat heard back', () {
64+
var able = true;
65+
final mine = RelayPresence(relays: [Uri.parse('ws://127.0.0.1:9')], signer: me, canServe: () => able);
66+
addTearDown(mine.close);
67+
// A relay sends a device its own beat, as it sends everyone's; the
68+
// one it said before it could no longer serve stays a minute fresh.
69+
mine.receive(here(me, 'online'));
70+
expect(mine.online(withinMs: 15000), {me.publicKey});
71+
able = false;
72+
expect(mine.online(withinMs: 15000), isEmpty, reason: 'a device that cannot serve is not here, whatever it heard of itself');
73+
able = true;
74+
mine.silent = true;
75+
expect(mine.online(withinMs: 15000), isEmpty, reason: 'nor while it is silent');
76+
});
6277
});
6378
}

0 commit comments

Comments
 (0)