Commit 4916f87658

4916f8765874e48e6457690c1a7d92f2316e74c3

parent: 21d1bae1db

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-21 10:30 UTC

Stop a js block org-sbe runs after five seconds

Layout: unified · split

Sources/OrgApp/JavaScriptBlocks.swift +19 −4
@@ -36,11 +36,15 @@ public enum JavaScriptBlocks {
3636 }
3737 }
3838
39 /// Runs a js `job` without a session on the calling thread, as `org-sbe` in a table
40 /// formula needs; nil for other jobs.
41 public static func runHere(_ job: BabelJob) throws -> BabelOutput? {
39 /// Runs a js `job` without a session, as `org-sbe` in a table formula needs, waiting on the
40 /// calling thread at most `timeout`; nil for other jobs.
41 public static func runHere(_ job: BabelJob, timeout: Duration = .seconds(5)) throws -> BabelOutput? {
4242 guard job.language == "js" || job.language == "javascript", case .process = job.runner else { return nil }
43 return try Engine().main(job.input, run: Run())
43 let run = Run()
44 let thread = Thread { run.end(Result { try Engine().main(job.input, run: run) }) }
45 thread.stackSize = 8 << 20
46 thread.start()
47 return try run.result(within: timeout)
4448 }
4549
4650 struct Stopped: Error, CustomStringConvertible {
@@ -71,6 +75,7 @@ public enum JavaScriptBlocks {
7175 private var result: Result<BabelOutput, Error>?
7276 private var continuation: CheckedContinuation<BabelOutput, Error>?
7377 private let wake = DispatchSemaphore(value: 0)
78 private let done = DispatchSemaphore(value: 0)
7479
7580 var stopped: Bool { lock.withLock { result != nil } }
7681
@@ -84,6 +89,16 @@ public enum JavaScriptBlocks {
8489 guard first else { return }
8590 waiting?.resume(with: outcome)
8691 wake.signal()
92 done.signal()
93 }
94
95 /// Blocks until the run ends, ending it when `timeout` passes first.
96 func result(within timeout: Duration) throws -> BabelOutput {
97 let (seconds, attoseconds) = timeout.components
98 if done.wait(timeout: .now() + .nanoseconds(Int(seconds) * 1_000_000_000 + Int(attoseconds / 1_000_000_000))) == .timedOut {
99 end(.failure(Stopped(description: "The block took longer than \(timeout) and was stopped.")))
100 }
101 return try lock.withLock { result! }.get()
87102 }
88103
89104 func wait(_ continuation: CheckedContinuation<BabelOutput, Error>) {
Sources/OrgApp/WorkspaceModel.swift +1 −1
@@ -66,7 +66,7 @@ public final class WorkspaceModel {
6666 }
6767 #if canImport(JavaScriptCore)
6868 // `org-sbe` in table formulas runs js blocks in JavaScriptCore.
69 TableBlocks.javaScript = JavaScriptBlocks.runHere
69 TableBlocks.javaScript = { try JavaScriptBlocks.runHere($0) }
7070 #endif
7171 }
7272
Tests/OrgAppTests/JavaScriptBlocksTests.swift +11 −1
@@ -267,10 +267,20 @@ struct JavaScriptBlocksTests {
267267 await #expect(throws: JavaScriptBlocks.Stopped.self) { try await JavaScriptBlocks.run(interval, directory: folder, timeout: .milliseconds(100)) }
268268 }
269269
270 /// A block `org-sbe` runs that doesn't finish is stopped, so recalculating can't hang.
271 @Test func runHereStopsAtItsDeadline() throws {
272 let start = ContinuousClock.now
273 let interval = try Self.plan("#+begin_src js\nsetInterval(() => {}, 10)\n#+end_src\n")
274 #expect(throws: JavaScriptBlocks.Stopped.self) { try JavaScriptBlocks.runHere(interval, timeout: .milliseconds(100)) }
275 #expect(ContinuousClock.now - start < .seconds(2))
276 let quick = try Self.plan("#+begin_src js\nreturn 6*7\n#+end_src\n")
277 #expect(try JavaScriptBlocks.runHere(quick)?.stdout.contains("42") == true)
278 }
279
270280 /// `org-sbe` on js blocks in a table formula, as Emacs runs them with node.
271281 @Test(.enabled(if: oracle && nodeInstalled))
272282 func tableFormulasMatchEmacs() throws {
273 TableBlocks.javaScript = JavaScriptBlocks.runHere
283 TableBlocks.javaScript = { try JavaScriptBlocks.runHere($0) }
274284 let text = """
275285 #+NAME: sq
276286 #+begin_src js :var n=2