Commit7ced8087Recorded5 Nov 2026Repositorysigil-dbus

Bound subscription setup and detach callbacks during cleanup

Changed
 README.md                     |  9 ++++++++-
 src/sigil/dbus/connection.sgl | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
 test/test-integration.sgl     | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+), 23 deletions(-)
Diff
README.mdmodified
@@ -125,7 +125,14 @@ already processed by the pump; `dbus-unique-name` is set by Hello.
125
```
126
127
Subscription keys are `sender`, `interface`, `member`, `path`, `destination`,
−128
and `arg0`. AddMatch/RemoveMatch are handled for you. Well-known senders are
+128
and `arg0`. AddMatch/RemoveMatch are handled for you. Subscription setup accepts
+129
`timeout-ms:` for one total budget and `await:` for a cancelable pending-call
+130
waiter (default `dbus-await`). A failed setup removes possible late matches with
+131
no reply wait, using `cleanup-timeout-ms:` (default 10ms). Unsubscribe detaches
+132
the local callback immediately, even on failure; it accepts the same timeout
+133
and await options. With `wait?: #f`, it sends bounded no-reply removals instead
+134
of waiting for acknowledgments. Remote cleanup is best effort if the connection
+135
fails; closing it releases its remaining daemon matches. Well-known senders are
136
resolved and followed across ownership changes. Callbacks receive a complete
137
message and may inspect `dbus-message-body`, `dbus-message-signature`, and
138
`dbus-message-field`. Exceptions in signal callbacks are contained.
src/sigil/dbus/connection.sglmodified
@@ -197,7 +197,31 @@
197
(and (equal? actual (if (eq? 'sender (car entry)) owner (cdr entry)))
198
(loop (cdr rest)))))))
199
−200
(define (dbus-subscribe! c matches handler)
+200
;; Match setup shares one budget; callers may supply a cancelable await.
+201
(define (match-call c member rule deadline await)
+202
(if (<= (dbus-remaining-ms deadline) 0)
+203
(dbus-error "org.freedesktop.DBus.Error.NoReply" "Subscription timed out")
+204
(await (dbus-call-async c bus-name bus-path bus-name member "s" (list rule)
+205
timeout-ms: (dbus-remaining-ms deadline)))))
+206
+207
(define (remove-matches! c rules timeout-ms)
+208
(let ((deadline (dbus-deadline timeout-ms)))
+209
(for-each
+210
(lambda (rule)
+211
(when rule
+212
(send-message! c
+213
(dbus-message 1 (next-serial! c)
+214
(list (cons 1 (dbus-variant "o" bus-path))
+215
(cons 2 (dbus-variant "s" bus-name))
+216
(cons 3 (dbus-variant "s" "RemoveMatch"))
+217
(cons 6 (dbus-variant "s" bus-name)))
+218
"s" (list rule) flags: 1)
+219
(max 1 (dbus-remaining-ms deadline)))))
+220
rules)))
+221
+222
(define (dbus-subscribe! c matches handler
+223
(keys: (timeout-ms 25000) (await dbus-await)
+224
(cleanup-timeout-ms 10)))
225
(when (>= (length (vector-ref c 3)) 1024) (error "Too many D-Bus subscriptions"))
226
(let* ((rule (match-rule matches))
227
(sender (assoc 'sender matches))
@@ -207,32 +231,51 @@
231
(match-rule (list (cons 'sender bus-name) (cons 'interface bus-name)
232
(cons 'member "NameOwnerChanged") (cons 'arg0 (cdr sender))))))
233
(token (vector matches handler rule owner-rule (and sender (cdr sender))))
−210
(owner-added? #f)
−211
(reply (dbus-call c bus-name bus-path bus-name "AddMatch" "s" (list rule))))
−212
(if (dbus-error? reply)
−213
reply
−214
(guard (e (else
−215
(dbus-call c bus-name bus-path bus-name "RemoveMatch" "s" (list rule))
−216
(when owner-added?
−217
(dbus-call c bus-name bus-path bus-name "RemoveMatch" "s" (list owner-rule)))
−218
(raise e)))
+234
(deadline (dbus-deadline timeout-ms))
+235
(possible-rules '()))
+236
(define (add! added-rule)
+237
(let ((remaining (dbus-remaining-ms deadline)))
+238
(when (<= remaining 0)
+239
(raise (dbus-error "org.freedesktop.DBus.Error.NoReply" "Subscription timed out")))
+240
(let ((pending (dbus-call-async c bus-name bus-path bus-name "AddMatch" "s"
+241
(list added-rule) timeout-ms: remaining)))
+242
(set! possible-rules (cons added-rule possible-rules))
+243
(let ((reply (await pending)))
+244
(when (dbus-error? reply)
+245
(unless (equal? "org.freedesktop.DBus.Error.NoReply" (dbus-error-name reply))
+246
(set! possible-rules (remove-if (lambda (r) (equal? r added-rule)) possible-rules)))
+247
(raise reply))))))
+248
(guard (e (else
+249
;; Ordered no-reply removals also cover a late AddMatch reply.
+250
(guard (cleanup-error (else #f))
+251
(remove-matches! c possible-rules cleanup-timeout-ms))
+252
(if (dbus-error? e) e (raise e))))
+253
(add! rule)
254
(when well-known?
−220
(let ((added (dbus-call c bus-name bus-path bus-name "AddMatch" "s" (list owner-rule))))
−221
(when (dbus-error? added) (raise added))
−222
(set! owner-added? #t))
−223
(let ((owner (dbus-call c bus-name bus-path bus-name "GetNameOwner" "s" (list (cdr sender)))))
+255
(add! owner-rule)
+256
(let ((owner (match-call c "GetNameOwner" (cdr sender) deadline await)))
+257
(when (and (dbus-error? owner)
+258
(not (equal? "org.freedesktop.DBus.Error.NameHasNoOwner"
+259
(dbus-error-name owner))))
+260
(raise owner))
261
(vector-set! token 4 (and (not (dbus-error? owner)) (car owner)))))
262
(vector-set! c 3 (cons token (vector-ref c 3)))
−226
token))))
+263
token)))
264
−228
(define (dbus-unsubscribe! c token)
+265
(define (dbus-unsubscribe! c token
+266
(keys: (timeout-ms 25000) (await dbus-await) (wait? #t)))
267
(when (memq token (vector-ref c 3))
−230
(let ((reply (dbus-call c bus-name bus-path bus-name "RemoveMatch" "s"
−231
(list (vector-ref token 2)))))
−232
(unless (dbus-error? reply)
−233
(when (vector-ref token 3)
−234
(dbus-call c bus-name bus-path bus-name "RemoveMatch" "s" (list (vector-ref token 3))))
−235
(vector-set! c 3 (remove-if (lambda (s) (eq? s token)) (vector-ref c 3)))))))
+268
;; Local delivery stops even if the daemon never acknowledges removal.
+269
(vector-set! c 3 (remove-if (lambda (s) (eq? s token)) (vector-ref c 3)))
+270
(if wait?
+271
(let ((deadline (dbus-deadline timeout-ms)))
+272
(for-each
+273
(lambda (rule)
+274
(when rule
+275
(let ((reply (match-call c "RemoveMatch" rule deadline await)))
+276
(when (dbus-error? reply) (raise reply)))))
+277
(list (vector-ref token 2) (vector-ref token 3))))
+278
(remove-matches! c (list (vector-ref token 2) (vector-ref token 3)) timeout-ms))))
279
280
(define (safe-callback c thunk)
281
(when (< (vector-ref c 8) 64)
test/test-integration.sglmodified
@@ -141,6 +141,56 @@
141
(when (and (null? seen) (< n 20)) (dbus-pump! client timeout-ms: 5) (sleep 0.001) (loop (+ n 1))))
142
(assert-equal (list iface (list (cons "Counter" (dbus-variant "u" 8))) '()) (car seen))
143
(dbus-unsubscribe! client token)))
+144
(test "subscription control uses the supplied await and one total deadline"
+145
(let ((calls 0) (before (length (vector-ref client 3))))
+146
(let ((result
+147
(dbus-subscribe! client
+148
(list (cons 'sender service-name) (cons 'interface iface)) (lambda (m) #f)
+149
timeout-ms: 20
+150
await: (lambda (pending)
+151
(set! calls (+ calls 1))
+152
(sleep 0.03)
+153
(dbus-await pending)))))
+154
(assert-true (dbus-error? result))
+155
(assert-equal "org.freedesktop.DBus.Error.NoReply" (dbus-error-name result))
+156
(assert-equal 1 calls)
+157
(assert-equal before (length (vector-ref client 3))))))
+158
(test "well-known subscription stages share their remaining budget"
+159
(let ((calls 0))
+160
(let ((result
+161
(dbus-subscribe! client
+162
(list (cons 'sender service-name) (cons 'interface iface)) (lambda (m) #f)
+163
timeout-ms: 20
+164
await: (lambda (pending)
+165
(set! calls (+ calls 1))
+166
(sleep 0.012)
+167
(dbus-await pending)))))
+168
(assert-true (dbus-error? result))
+169
(assert-equal "org.freedesktop.DBus.Error.NoReply" (dbus-error-name result))
+170
(assert-true (<= calls 2)))))
+171
(test "canceled subscription setup leaves no local callback"
+172
(let ((before (length (vector-ref client 3))))
+173
(assert-error
+174
(dbus-subscribe! client (list (cons 'interface iface)) (lambda (m) #f)
+175
await: (lambda (pending) (error "Canceled test subscription"))))
+176
(assert-equal before (length (vector-ref client 3)))
+177
(assert-equal '("alive")
+178
(dbus-call client service-name path iface "Echo" "s" '("alive")))))
+179
(test "unsubscribe detaches locally when its await fails"
+180
(let* ((seen 0)
+181
(token (dbus-subscribe! client (list (cons 'interface iface))
+182
(lambda (m) (set! seen (+ seen 1))))))
+183
(assert-error (dbus-unsubscribe! client token
+184
await: (lambda (pending) (error "Canceled removal"))))
+185
(assert-false (memq token (vector-ref client 3)))
+186
(dbus-send-signal! server path iface "Changed" "u" '(1))
+187
(dbus-call client bus bus-path bus "ListNames" "" '())
+188
(assert-equal 0 seen)))
+189
(test "no-reply unsubscribe does not call await"
+190
(let ((token (dbus-subscribe! client (list (cons 'interface iface)) (lambda (m) #f))))
+191
(dbus-unsubscribe! client token timeout-ms: 10 wait?: #f
+192
await: (lambda (pending) (error "Unexpected wait")))
+193
(assert-false (memq token (vector-ref client 3)))))
194
(test "read-only property"
195
(assert-equal "org.freedesktop.DBus.Error.PropertyReadOnly"
196
(dbus-error-name (dbus-call client service-name path props "Set" "ssv"