~williewillus/public-inbox

r16: Simplify implementation of off-thread v1 APPLIED

Vincent Lee: 1
 Simplify implementation of off-thread

 1 files changed, 10 insertions(+), 12 deletions(-)
#713742 linux_buildtest.yml success
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~williewillus/public-inbox/patches/30199/mbox | git am -3
Learn more about email & git

[PATCH r16] Simplify implementation of off-thread Export this patch

The original implementation used channel-put-event to avoid an obscure attack whereby the
code in the sandbox could hold up the worker thread by exiting before reading the response
from the channel.

This diff switches the implementation to buffered async channels which don't block on put
(up to the buffer size limit of 1).  Makes the implementation quite a bit easier to read.

Tested locally.
---
 frontends/discord.rkt | 22 ++++++++++------------
 1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/frontends/discord.rkt b/frontends/discord.rkt
index 6e50313..11f78c9 100644
--- a/frontends/discord.rkt
+++ b/frontends/discord.rkt
@@ -2,6 +2,7 @@

(require
 (only-in net/url get-pure-port string->url)
 racket/async-channel
 racket/class
 racket/contract
 (only-in racket/format ~a)
@@ -34,24 +35,21 @@
(define (message-author-id message)
  (hash-ref (hash-ref message 'author) 'id))

; Defines a function that delegates to an external worker thread.
; Used to expose limited IO capabilities to inside of a sandbox, as body
; runs outside of the sandbox
(define-syntax-rule (define/off-thread (name args ...)
                      body ...)
  (begin
    (define worker-thread
      (thread-loop
       (match (thread-receive)
         [(vector chan args ...)
          (define res (let () body ...))
          (sync/timeout 1 (channel-put-evt chan res))]
         [_ (void)])))
       (match-define (vector chan args ...) (thread-receive))
       (define res (let () body ...))
       (async-channel-put chan res)))
    (define (name args ...)
      (define chan (make-channel))
      (sync
       chan
       (guard-evt
        (thunk
         (thread-send worker-thread (vector chan args ...))
         never-evt))))))
      (define chan (make-async-channel 1))
      (thread-send worker-thread (vector chan args ...))
      (async-channel-get chan))))

(define discord-frontend%
  (class* object% [r16-frontend<%>]
-- 
2.35.1
r16/patches/linux_buildtest.yml: SUCCESS in 1m56s

[Simplify implementation of off-thread][0] from [Vincent Lee][1]

[0]: https://lists.sr.ht/~williewillus/public-inbox/patches/30199
[1]: mailto:vincent@vincent-lee.net

✓ #713742 SUCCESS r16/patches/linux_buildtest.yml https://builds.sr.ht/~williewillus/job/713742
Eutro's ok-ed this with the request that I use double semicolons for
comments. Will do that in a separate commit, but marking this one as applied.