Commit39eaae9bRecorded5 Nov 2026Repositorysigil-dbus
Port connection readiness to the released SDK graph
Changed
README.md | 8 ++++++++
package.sgl | 6 +++---
src/sigil/dbus.sgl | 2 +-
src/sigil/dbus/connection.sgl | 3 ++-
src/sigil/dbus/transport.sgl | 11 +++++++----
test/test-readiness.sgl | 46 ++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 67 insertions(+), 9 deletions(-)Diff
README.mdmodified
@@ -50,6 +50,14 @@ There is no hidden background reader: services and signal-only clients must
50
run a pump, usually in `(go (dbus-run! connection))`. Call `dbus-close!` to 51
release the socket and resolve outstanding calls as Disconnected. 52
+53
`dbus-connection-fd` returns the live native descriptor, or `#f` after close.+54
The connection retains ownership: use it only for readiness waits, never+55
read, write, or close it directly. After readiness, `dbus-pump!` with+56
`timeout-ms: 0` processes one available frame without waiting; partial frames+57
remain buffered for the next readiness notification. A caller with pending+58
calls must also wake for their deadlines. Cancel a wait before closing its+59
connection or reusing descriptors.+60
61
## Export a service 62
63
```schemepackage.sglmodified
@@ -6,7 +6,7 @@
6
license: "BSD-3-Clause" 7
entry: '(sigil dbus) 8
dependencies: (list−9
(from-pkg name: "sigil/sigil-stdlib" version: "=0.24.2"))+9
(from-pkg name: "sigil/sigil-stdlib" version: "=0.24.4")) 10
dev-dependencies: (list−11
(from-pkg name: "sigil/sigil-test" version: "=0.24.2")−12
(from-pkg name: "sigil/sigil-test-runner" version: "=0.24.2")))+11
(from-pkg name: "sigil/sigil-test" version: "=0.24.4")+12
(from-pkg name: "sigil/sigil-test-runner" version: "=0.24.4")))src/sigil/dbus.sglmodified
@@ -6,7 +6,7 @@
6
dbus-error dbus-error? dbus-error-name dbus-error-message 7
dbus-parse-signature dbus-marshal dbus-unmarshal dbus-native-endianness 8
dbus-max-message-size dbus-max-array-size−9
dbus-connect dbus-connect-session dbus-connect-system dbus-close! dbus-closed?+9
dbus-connect dbus-connect-session dbus-connect-system dbus-close! dbus-closed? dbus-connection-fd 10
dbus-unique-name dbus-owned-names dbus-call dbus-call-async dbus-await 11
dbus-pending? dbus-pending-ready? dbus-pump! dbus-run! 12
dbus-request-name! dbus-release-name! dbus-allow-replacementsrc/sigil/dbus/connection.sglmodified
@@ -3,7 +3,7 @@
3
(sigil dbus wire) (sigil dbus sensitive) 4
(sigil async) (sigil process) (sigil string)) 5
(export dbus-connect dbus-connect-session dbus-connect-system dbus-close!−6
dbus-closed? dbus-unique-name dbus-owned-names dbus-call dbus-call-async+6
dbus-closed? dbus-connection-fd dbus-unique-name dbus-owned-names dbus-call dbus-call-async 7
dbus-await dbus-pending? dbus-pending-ready? dbus-pump! dbus-run! 8
dbus-request-name! dbus-release-name! dbus-subscribe! dbus-unsubscribe! 9
dbus-send-signal! dbus-send-reply! dbus-set-dispatcher!@@ -20,6 +20,7 @@
20
;; method dispatcher, unique name, owned names, read lock, active callbacks. 21
;; No hidden reader fiber: await, pump, and run cooperate on this lock. 22
(define (dbus-closed? c) (dbus-transport-closed? (vector-ref c 0)))+23
(define (dbus-connection-fd c) (dbus-transport-fd (vector-ref c 0))) 24
(define (dbus-unique-name c) (vector-ref c 5)) 25
(define (dbus-owned-names c) (vector-ref c 6)) 26
(define (dbus-set-dispatcher! c dispatcher) (vector-set! c 4 dispatcher))src/sigil/dbus/transport.sglmodified
@@ -2,7 +2,7 @@
2
(import (sigil socket) (sigil async) (sigil time) (sigil process) 3
(sigil string) (sigil io) (sigil dbus message)) 4
(export dbus-open-transport dbus-close-transport! dbus-transport-read!−5
dbus-transport-write! dbus-transport-closed? dbus-parse-address+5
dbus-transport-write! dbus-transport-closed? dbus-transport-fd dbus-parse-address 6
dbus-deadline dbus-remaining-ms) 7
(begin 8
(define (dbus-deadline timeout-ms)@@ -60,6 +60,8 @@
60
;; write lock. One reader is coordinated by the connection's pump. 61
(define (dbus-transport-closed? transport) 62
(socket-closed? (vector-ref transport 0)))+63
(define (dbus-transport-fd transport)+64
(and (not (dbus-transport-closed? transport)) (socket-fd (vector-ref transport 0)))) 65
66
(define (dbus-close-transport! transport) 67
(socket-close (vector-ref transport 0)))@@ -189,15 +191,16 @@
191
(vector-set! transport 2 0) 192
(vector-set! transport 3 #t) 193
(dbus-decode-message buffer max-bytes: (vector-ref transport 4)))))−192
((<= (dbus-remaining-ms deadline) 0) #f)+194
((and (> timeout-ms 0) (<= (dbus-remaining-ms deadline) 0)) #f) 195
(else 196
(let ((chunk (socket-read-bytevector sock (min remaining 65536)))) 197
(cond 198
((not chunk) (error "D-Bus socket read failed")) 199
((eof-object? chunk) (error "D-Bus connection EOF")) 200
((= 0 (bytevector-length chunk))−199
(await-readable-any (list sock) timeout-ms: (dbus-remaining-ms deadline))−200
(loop))+201
(if (= timeout-ms 0) #f+202
(begin (await-readable-any (list sock) timeout-ms: (dbus-remaining-ms deadline))+203
(loop)))) 204
(else 205
(bytevector-copy! buffer used chunk) 206
(vector-set! transport 2 (+ used (bytevector-length chunk)))test/test-readiness.sgladded
@@ -0,0 +1,46 @@
+1
#!/usr/bin/env sigil+2
;;; A real private bus proves nonblocking pumps consume ready messages.+3
(import (sigil dbus) (sigil async))+4
(requires (tools dbus-daemon))+5
(define checked 0)+6
(define (check value message)+7
(unless value (error message)) (set! checked (+ checked 1)))+8
(with-temp-dir root+9
(define address (str "unix:path=" (path root "bus")))+10
(define config (path root "bus.conf"))+11
(write-text config (str "<busconfig><type>session</type><listen>" address "</listen><auth>EXTERNAL</auth>"+12
"<policy context=\"default\"><allow user=\"*\"/><allow own=\"*\"/>"+13
"<allow send_destination=\"*\"/><allow receive_sender=\"*\"/></policy></busconfig>"))+14
(define bus #f)+15
(define receiver #f)+16
(define sender #f)+17
(dynamic-wind+18
(lambda () #t)+19
(lambda ()+20
(set! bus (spawn dbus-daemon --nofork --config-file ,config out: ,(path root "bus.log") err: 'merge))+21
(wait-until (file-exists? (path root "bus")) timeout: 5)+22
(set! receiver (dbus-connect address)) (set! sender (dbus-connect address))+23
(define fd (dbus-connection-fd receiver))+24
(check (and (exact-integer? fd) (>= fd 0)) "Connection descriptor was unavailable")+25
(define seen '())+26
(dbus-subscribe! receiver+27
(list (cons 'sender (dbus-unique-name sender)) (cons 'interface "org.example.Readiness"))+28
(lambda (message) (set! seen (append seen (dbus-message-body message)))))+29
(let drain ((left 32)) (when (and (> left 0) (dbus-pump! receiver timeout-ms: 0)) (drain (- left 1))))+30
(for-each (lambda (value)+31
(dbus-send-signal! sender "/org/example/Readiness" "org.example.Readiness" "Changed" "s" (list value)))+32
'("first" "second" "third"))+33
(define readiness (await-readable-fds (list fd) timeout-ms: 2000))+34
(check (and readiness (memv fd (car readiness))) "Socket readiness did not wake")+35
(wait-until+36
(begin (dbus-pump! receiver timeout-ms: 0) (= 3 (length seen))) timeout: 2 interval: 0.01)+37
(check (equal? seen '("first" "second" "third")) "Zero-timeout pump lost or reordered frames")+38
(define started (current-milliseconds))+39
(for-each (lambda (ignored) (dbus-pump! receiver timeout-ms: 0)) '(1 2 3 4 5 6 7 8))+40
(check (< (- (current-milliseconds) started) 250) "Zero-timeout pump waited without data")+41
(dbus-close! receiver)+42
(check (not (dbus-connection-fd receiver)) "Closed descriptor remained public"))+43
(lambda ()+44
(when receiver (dbus-close! receiver)) (when sender (dbus-close! sender))+45
(when bus (when (process-alive? bus) (process-kill! bus)) (process-wait bus)))))+46
(println "PASS ~a D-Bus readiness checks" checked)