Skip to content

Commit e9b7a55

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 e9b7a55

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ _When adding new entries to the changelog, please include issue/PR numbers where
66

77
## Unreleased
88

9+
- diff: Fixed garbled json-lines output sometimes when using `--add-feature-count-estimate` [#1040](https://github.com/koordinates/kart/issues/1040)
910
- diff/show: Faster output for some large repositories (varies wildly) [#1038](https://github.com/koordinates/kart/issues/1038)
1011
- show: Added `--no-sort-keys` option to disable sorting of features by name/PK. Previously added to `diff` only
1112
- show: Added `--add-feature-count-estimate` option to add a feature count estimate to `json-lines` output. Previously added to `diff` only

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)