From 08d51ffd9f4b412a2a0db37020d8b1c8dd93c0f1 Mon Sep 17 00:00:00 2001 From: sam-perez Date: Sun, 19 Oct 2025 23:42:30 -0700 Subject: [PATCH 1/6] Update the server to "atomically" write from polling reader's perspective. --- src/server.rb | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/server.rb b/src/server.rb index d5876cf7..584b151c 100644 --- a/src/server.rb +++ b/src/server.rb @@ -163,5 +163,32 @@ end end -File.write(connection_filepath, JSON.fast_generate(connection_information)) +# Write connection information atomically so readers never see a partial file +begin + json = JSON.fast_generate(connection_information) + + dir = File.dirname(connection_filepath) + base = File.basename(connection_filepath) + tmp_path = File.join(dir, ".#{base}.tmp-#{Process.pid}") + + # Write to a temp file in the same directory as the final target + File.open(tmp_path, "wb") do |f| + f.write(json) + f.flush + f.fsync # ensure contents are on disk before swap + end + + # Atomic swap into place (same filesystem) + File.rename(tmp_path, connection_filepath) +rescue Exception => e + # Best-effort cleanup of the temp file + begin + File.unlink(tmp_path) if tmp_path && File.exist?(tmp_path) + rescue StandardError + # ignore + end + raise e +end + listener.join + From 104b468dc136e39b514cd46183b69e9beab660c8 Mon Sep 17 00:00:00 2001 From: sam-perez Date: Mon, 20 Oct 2025 00:09:33 -0700 Subject: [PATCH 2/6] TESTING Added debug output for file writing process, useful only for testing. --- src/server.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/server.rb b/src/server.rb index 584b151c..a6e7cb4d 100644 --- a/src/server.rb +++ b/src/server.rb @@ -171,15 +171,27 @@ base = File.basename(connection_filepath) tmp_path = File.join(dir, ".#{base}.tmp-#{Process.pid}") + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + puts "WRITING TEMP FILE: #{tmp_path}" + puts "CONTENT: #{json}" + # Write to a temp file in the same directory as the final target File.open(tmp_path, "wb") do |f| f.write(json) f.flush - f.fsync # ensure contents are on disk before swap + # ensure contents are on disk before swap + f.fsync end + puts "SWAPPING IN PLACE #{tmp_path} and #{connection_filepath}" # Atomic swap into place (same filesystem) File.rename(tmp_path, connection_filepath) + puts "DONE WRITING FILE: #{connection_filepath}" + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" rescue Exception => e # Best-effort cleanup of the temp file begin From a13bf94f670c7115b720941d23caa5c814ebd07c Mon Sep 17 00:00:00 2001 From: sam-perez Date: Mon, 20 Oct 2025 00:28:21 -0700 Subject: [PATCH 3/6] Passthrough standard out, see if we get the output. --- src/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugin.js b/src/plugin.js index 71d2030b..8a3308ee 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -70,7 +70,7 @@ export async function spawnServer(opts, killOnExit = true) { const options = { env: Object.assign({}, process.env, { LANG: getLang() }), - stdio: ["ignore", "ignore", "inherit"], + stdio: ["ignore", "inherit", "inherit"], detached: true }; From 59b1e61e9eba7e76951978bf269534411c44c8d0 Mon Sep 17 00:00:00 2001 From: sam-perez Date: Mon, 20 Oct 2025 09:53:39 -0700 Subject: [PATCH 4/6] Remove logging. See if linting is working. --- package.json | 2 +- src/plugin.js | 2 +- src/server.rb | 15 ++------------- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index 69dea870..751ba376 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type": "module", "main": "src/plugin.js", "scripts": { - "checkFormat": "prettier . --check", + "checkFormat": "prettier . --check --diff", "lint": "eslint --cache .", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }, diff --git a/src/plugin.js b/src/plugin.js index 8a3308ee..71d2030b 100644 --- a/src/plugin.js +++ b/src/plugin.js @@ -70,7 +70,7 @@ export async function spawnServer(opts, killOnExit = true) { const options = { env: Object.assign({}, process.env, { LANG: getLang() }), - stdio: ["ignore", "inherit", "inherit"], + stdio: ["ignore", "ignore", "inherit"], detached: true }; diff --git a/src/server.rb b/src/server.rb index a6e7cb4d..9446f16d 100644 --- a/src/server.rb +++ b/src/server.rb @@ -171,12 +171,6 @@ base = File.basename(connection_filepath) tmp_path = File.join(dir, ".#{base}.tmp-#{Process.pid}") - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - puts "WRITING TEMP FILE: #{tmp_path}" - puts "CONTENT: #{json}" - # Write to a temp file in the same directory as the final target File.open(tmp_path, "wb") do |f| f.write(json) @@ -185,20 +179,15 @@ f.fsync end - puts "SWAPPING IN PLACE #{tmp_path} and #{connection_filepath}" - # Atomic swap into place (same filesystem) + # Atomic swap into place via file rename File.rename(tmp_path, connection_filepath) - puts "DONE WRITING FILE: #{connection_filepath}" - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" - puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" rescue Exception => e - # Best-effort cleanup of the temp file begin File.unlink(tmp_path) if tmp_path && File.exist?(tmp_path) rescue StandardError # ignore end + raise e end From 81060debb1f3f6da03ed5fc9e9c519bee06afd1d Mon Sep 17 00:00:00 2001 From: sam-perez Date: Mon, 20 Oct 2025 10:00:48 -0700 Subject: [PATCH 5/6] No diff needed? --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 751ba376..69dea870 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "type": "module", "main": "src/plugin.js", "scripts": { - "checkFormat": "prettier . --check --diff", + "checkFormat": "prettier . --check", "lint": "eslint --cache .", "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js" }, From d1621902acddf4f030220a2b40738021c08e9144 Mon Sep 17 00:00:00 2001 From: sam-perez Date: Mon, 20 Oct 2025 10:04:50 -0700 Subject: [PATCH 6/6] Extra newline? --- src/server.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/src/server.rb b/src/server.rb index 9446f16d..8c03c151 100644 --- a/src/server.rb +++ b/src/server.rb @@ -192,4 +192,3 @@ end listener.join -