Skip to content

Commit f0d4d07

Browse files
committed
Fix garbled JSON-lines output sometimes
In `kart diff -o json-lines --add-feature-count-estimate={value}`, two threads would write to the output. This correctly used a lock to prevent races, but there is an intermediate bytearray which is written just beforehand (introduced with #1025) and this also needed to use a lock to prevent races. This change moves that intermediate bytearray under the same lock as writing the output buffer, thus avoiding the race condition
1 parent 943e87a commit f0d4d07

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

kart/json_diff_writers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ def __init__(self, *args, diff_estimate_accuracy=None, delta_filter=None, **kwar
247247
self._output_buffer = bytearray()
248248

249249
def dump(self, obj):
250-
# https://jcristharif.com/msgspec/perf-tips.html#line-delimited-json
251-
msgspec_json_encoder.encode_into(obj, self._output_buffer)
252-
self._output_buffer.extend(b"\n")
253250
with self._output_lock:
251+
# https://jcristharif.com/msgspec/perf-tips.html#line-delimited-json
252+
msgspec_json_encoder.encode_into(obj, self._output_buffer)
253+
self._output_buffer.extend(b"\n")
254254
self.fp.buffer.write(self._output_buffer)
255255

256256
def write_header(self):

0 commit comments

Comments
 (0)