diff --git a/scripts/library_run.py b/scripts/library_run.py index 7ea9f80..50b192e 100644 --- a/scripts/library_run.py +++ b/scripts/library_run.py @@ -1575,7 +1575,43 @@ def process(src, args, dest=None, origin=None): else: return last, None # 6. atomic replace, then confirm on the share - os.replace(remote_tmp, live) + # + # The upload's write handle is not gone the moment `copy_local_to_remote` returns: an SMB + # client can hold it for a second or two, and the rename then dies with WinError 32 ("being + # used by another process") for a file nobody else has open. That is what failed the + # `.mov -> .mp4` install of one 19 MB clip three times in a row (measured 2026-10-28 on + # Z:\Temporary\短视频\_io-test) while a hand-run os.replace in the SAME directory + # succeeded - so it is the leftover handle, not the share's permissions. Retry a few times + # instead of failing an item that is already encoded AND verified. + for _try in range(1, 6): + try: + os.replace(remote_tmp, live) + break + except OSError as e: + if _try == 5: + # A rename that cannot be satisfied at all is a PHANTOM staged name, not a + # busy file: the share's local cache reported `...new.mp4` as existing while the + # server had already forgotten it (measured 2026-10-28: os.path.exists() said + # yes, `ls` of the same directory did not list it, and every rename - including + # a hand-run one - failed with WinError 32). Retrying cannot fix that. We still + # HOLD the verified local encode, so install it straight under the final name + # and hash-check it, instead of failing an item that is already done. + log(" ! rename is impossible on this share (%s) - installing %s directly" + % (str(e)[:70], os.path.basename(live))) + if not copy_local_to_remote(loc_out, live): + return ("FAIL install: the share refused both the rename (%s) and a direct " + "write of %s" % (str(e)[:70], live)), None + h_re, n_re = _sha_remote(live) + if (h_re, n_re) != (h_loc, n_loc): + return ("FAIL install read-back: %s is %s/%d, encoded was %s/%d" + % (live, h_re[:12], n_re, h_loc[:12], n_loc)), None + try: + os.remove(remote_tmp) + except OSError: + pass + break + log(" ! rename held by the share (%s) - retry %d/5" % (str(e)[:70], _try)) + time.sleep(0.5 * _try) # NEVER delete the source when it came from the snapshot origin: that is the archive of # the originals, and removing it would destroy the only clean copy of the library. if not origin_used and not getattr(args, "keep_source", False) \