Commit dd4ac706e9
dd4ac706e9cf1f2735b3a614f7340555bf4bb062
parent: f6d7ffee07
Verified · cmc
cmc <hello@cleberg.net> · 2026-11-21 13:38 UTC
Don't start a js block org-sbe gave up on while it still runs
Layout: unified · split
Sources/OrgApp/JavaScriptBlocks.swift
+45 −3
| @@ -37,14 +37,56 @@ public enum JavaScriptBlocks { |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 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. |
| 40 | /// calling thread at most `timeout`; nil for other jobs. A block whose earlier run timed out |
| 41 | /// and is still running is refused without starting another. |
| 41 | 42 | public static func runHere(_ job: BabelJob, timeout: Duration = .seconds(5)) throws -> BabelOutput? { |
| 42 | 43 | guard job.language == "js" || job.language == "javascript", case .process = job.runner else { return nil } |
| 44 | if abandoned.contains(job.input) { throw Stopped(description: "The block took too long before and is still running.") } |
| 43 | 45 | let run = Run() |
| 44 | | let thread = Thread { run.end(Result { try Engine().main(job.input, run: run) }) } |
| 46 | let flag = Abandoned.Flag() |
| 47 | let thread = Thread { |
| 48 | run.end(Result { try Engine().main(job.input, run: run) }) |
| 49 | abandoned.ended(job.input, flag) |
| 50 | } |
| 45 | 51 | thread.stackSize = 8 << 20 |
| 46 | 52 | thread.start() |
| 47 | | return try run.result(within: timeout) |
| 53 | do { |
| 54 | return try run.result(within: timeout) |
| 55 | } catch let error as Stopped { |
| 56 | abandoned.add(job.input, flag) |
| 57 | throw error |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static let abandoned = Abandoned() |
| 62 | |
| 63 | /// The inputs of `runHere` blocks that timed out and whose threads still run. |
| 64 | final class Abandoned: @unchecked Sendable { |
| 65 | final class Flag: @unchecked Sendable { |
| 66 | var ended = false |
| 67 | var abandoned = false |
| 68 | } |
| 69 | |
| 70 | private let lock = NSLock() |
| 71 | private var running: [String: Int] = [:] |
| 72 | |
| 73 | func contains(_ input: String) -> Bool { lock.withLock { running[input] != nil } } |
| 74 | |
| 75 | func add(_ input: String, _ flag: Flag) { |
| 76 | lock.withLock { |
| 77 | guard !flag.ended else { return } |
| 78 | flag.abandoned = true |
| 79 | running[input, default: 0] += 1 |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func ended(_ input: String, _ flag: Flag) { |
| 84 | lock.withLock { |
| 85 | flag.ended = true |
| 86 | guard flag.abandoned else { return } |
| 87 | if let n = running[input] { running[input] = n > 1 ? n - 1 : nil } |
| 88 | } |
| 89 | } |
| 48 | 90 | } |
| 49 | 91 | |
| 50 | 92 | struct Stopped: Error, CustomStringConvertible { |
Tests/OrgAppTests/JavaScriptBlocksTests.swift
+12
| @@ -277,6 +277,18 @@ struct JavaScriptBlocksTests { |
| 277 | 277 | #expect(try JavaScriptBlocks.runHere(quick)?.stdout.contains("42") == true) |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | /// A block whose earlier run timed out and still runs isn't started again, so a later |
| 281 | /// recalculation neither waits nor leaves another thread busy; once it ends it runs again. |
| 282 | @Test func runHereRefusesABlockStillRunning() throws { |
| 283 | let spin = try Self.plan("#+begin_src js\nconst end = Date.now() + 1500; while (Date.now() < end) {}\nreturn 7\n#+end_src\n") |
| 284 | #expect(throws: JavaScriptBlocks.Stopped.self) { try JavaScriptBlocks.runHere(spin, timeout: .milliseconds(100)) } |
| 285 | let start = ContinuousClock.now |
| 286 | #expect(throws: JavaScriptBlocks.Stopped.self) { try JavaScriptBlocks.runHere(spin, timeout: .seconds(5)) } |
| 287 | #expect(ContinuousClock.now - start < .milliseconds(500)) |
| 288 | Thread.sleep(forTimeInterval: 2) |
| 289 | #expect(try JavaScriptBlocks.runHere(spin)?.stdout.contains("7") == true) |
| 290 | } |
| 291 | |
| 280 | 292 | /// `org-sbe` on js blocks in a table formula, as Emacs runs them with node. |
| 281 | 293 | @Test(.enabled(if: oracle && nodeInstalled)) |
| 282 | 294 | func tableFormulasMatchEmacs() throws { |
docs/manual/guide/10-tables.org
+1 −1
| @@ -600,7 +600,7 @@ Emacs Lisp blocks run in orgstar's interpreter, with dynamic binding unless the |
| 600 | 600 | has =:lexical yes= or =:lexical t=, and =js= blocks run in JavaScriptCore, everywhere and |
| 601 | 601 | without asking, since neither can reach files or other programs. After five seconds orgstar |
| 602 | 602 | stops waiting for a =js= block and the table needs Emacs; a block that never finishes |
| 603 | | keeps running in the background until orgstar quits. Blocks in other languages, blocks with =:cache yes=, |
| 603 | keeps running in the background until orgstar quits, and isn't started again while it runs. Blocks in other languages, blocks with =:cache yes=, |
| 604 | 604 | =:file= results or column and row names, other =:lexical= values, values that hold a |
| 605 | 605 | function, Lisp values with symbols in a variable with several values, a name given to |
| 606 | 606 | more than one element, in any case, when the first isn't a src block, references to an =ID= or |