Commit 17f529e
committed
fix: make LiveSync writes atomic instead of locking the runtime's reads
m_fileWriteMutex paired LiveSync's file writes against Runtime::ReadFileText so
the runtime would not compile a half-written hot-synced module. It could not
deliver that, for three independent reasons:
- It is a non-static member, so LiveSync locking one runtime excludes nothing on
a worker's thread - precisely the case that matters now that workers run on
their own detached threads.
- The lock was taken after prepareFile() deleted the file and the
FileOutputStream constructor truncated it, so it covered only fos.write and
not the window a reader actually hits.
- The finally block called unlock() even when lock() had never been reached (a
throw from mkdirs() or the FileOutputStream constructor). Unlocking a
std::mutex this thread does not own is undefined behaviour, and on bionic can
release a lock the JS thread holds inside ReadFileText.
Writes now land through a sibling temp file renamed over the target. rename(2)
within a directory is atomic, so a reader gets either the whole previous file or
the whole new one, on every runtime and every thread, with no shared state to
scope wrongly. That also closes the delete/truncate window, which no reader-side
lock could have covered, and takes a mutex off every debug module read.
java.nio.file's ATOMIC_MOVE is unavailable at minSdk 21, so this uses
File.createTempFile in the target's own directory plus File.renameTo, which maps
to rename(2) on every API level. The temp file must be a sibling: rename is only
atomic within one filesystem.
m_fileWriteMutex, Runtime::Lock/Unlock, both lock_guards, the two JNI entry
points and the private native lock/unlock declarations are removed.
com.tns.Runtime.lock()/unlock() remain as deprecated no-ops: they are public API
and external tooling may call them.
The test suite does not exercise LiveSync, so this is verified by the arm64-v8a
build and by the suite not regressing (1038/1038). Hot-sync itself needs a
manual check.1 parent 7c9c3db commit 17f529e
5 files changed
Lines changed: 42 additions & 62 deletions
File tree
- test-app
- app/src/debug/java/com/tns
- runtime/src/main
- cpp
- java/com/tns
Lines changed: 32 additions & 18 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
357 | 357 | | |
358 | 358 | | |
359 | 359 | | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
360 | 369 | | |
361 | | - | |
362 | | - | |
| 370 | + | |
| 371 | + | |
363 | 372 | | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
368 | 388 | | |
369 | | - | |
370 | | - | |
371 | 389 | | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
372 | 394 | | |
373 | 395 | | |
374 | 396 | | |
375 | | - | |
376 | | - | |
| 397 | + | |
| 398 | + | |
377 | 399 | | |
378 | 400 | | |
379 | 401 | | |
| |||
387 | 409 | | |
388 | 410 | | |
389 | 411 | | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | 412 | | |
399 | 413 | | |
400 | 414 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
335 | 335 | | |
336 | 336 | | |
337 | 337 | | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | 338 | | |
342 | 339 | | |
343 | 340 | | |
344 | 341 | | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | 342 | | |
349 | 343 | | |
350 | 344 | | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | 345 | | |
364 | 346 | | |
365 | 347 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | 148 | | |
152 | 149 | | |
153 | 150 | | |
| |||
392 | 389 | | |
393 | 390 | | |
394 | 391 | | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | 392 | | |
399 | 393 | | |
400 | 394 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
285 | 285 | | |
286 | 286 | | |
287 | 287 | | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | 288 | | |
303 | 289 | | |
304 | 290 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | 74 | | |
79 | 75 | | |
80 | 76 | | |
| |||
766 | 762 | | |
767 | 763 | | |
768 | 764 | | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
769 | 771 | | |
770 | | - | |
771 | 772 | | |
772 | 773 | | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
773 | 778 | | |
774 | | - | |
775 | 779 | | |
776 | 780 | | |
777 | 781 | | |
| |||
0 commit comments