GeezerVison is showing github.com — golive: keep the holder until T3 really runs; no holder when /proc/lo… · scuttletoby/t3code@1703f80
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/README.md +17-7Lines changed: 17 additions & 7 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -339,13 +339,23 @@ start: the new launcher then idled and verify waited for minutes. | 339 | 339 | and the next SSH launch would start a server on that home. So right after the stop the kit starts a holder, a | 340 | 340 | transient unit of its own (`t3-golive-hold-<hash of the home>`, running `t3-golive hold --home <home>`), that | 341 | 341 | takes the owner lock and keeps it until it is stopped; the run adopts it as its own lock. Every start the kit makes | 342 | | - goes through the same handover, so the launcher gets the home from the holder; a failed start, a failure before | 343 | | - the start, or a kill leaves the holder holding. The next run of the same `back` takes the holder over (no free | 344 | | - moment, never a second one: there is one unit name per home); a resumed verify stops one that is still there once | 345 | | - the service runs T3. `status` and `check` name the holder; `settle` stops it (the owner says the home is right); | 346 | | - a reboot ends it. It is found by its live process in `/proc/locks`, never by a file, so nothing stale outlives | 347 | | - it. Where no transient unit can be started, the run holds the lock itself, as before, and the stop step says so | 348 | | - (`holder: null`, `holderError`). A copy needs none: its guard starts T3 again as soon as the copy run dies. | | 342 | + goes through the same handover, so the launcher gets the home from the holder. It goes only to a launcher that | | 343 | + holds its serving lock, or once the service runs: a failed start, a start that returns without T3 running (`t3 | | 344 | + update` exits non-zero or times out, a build that never comes up), a failure before the start, or a kill leaves | | 345 | + the holder holding. The next run of the same `back` takes the holder over (no free moment, never a second one: | | 346 | + there is one unit name per home); a resumed verify stops one that is still there once the service runs T3. | | 347 | + `status` and `check` name the holder (do not start T3 by hand while it holds: its server would idle); `settle` | | 348 | + stops it (the owner says the home is right). It is found by its live process in `/proc/locks`, never by a file, | | 349 | + so nothing stale outlives it. Where `/proc/locks` cannot be read (the kit could never find the holder again) or | | 350 | + no transient unit can be started, the run holds the lock itself, as before, and the stop step says so (`holder: | | 351 | + null`, `holderError`); `status` and `check` name an active holder unit they cannot see there. A copy needs none: | | 352 | + its guard starts T3 again as soon as the copy run dies. | | 353 | +- **Known gap: a reboot ends the holder.** The service stays disabled, so nothing starts T3 at boot, but a T3 | | 354 | + desktop app connected over SSH that reconnects after the reboot finds the home free and may start its own server | | 355 | + on it while it may be half-restored. A holder that outlives a reboot would be an enabled unit on disk, state that | | 356 | + can outlive its reason (what finding the holder by its live process avoids), so the kit keeps the gap and says | | 357 | + it: `status` then fails `home holder` (none, but a run that stopped and disabled T3 has not ended ok) and says to | | 358 | + quit or disconnect that app until the same `back` ends ok. | 349 | 359 | - **Backstop, a foreign server.** An SSH client older than 0.1.10 reads neither, and a server started by hand | 350 | 360 | ignores both. When the runtime record names a live process that the boot service did not start and that itself | 351 | 361 | holds `userdata/server-owner.sqlite` (a stale record whose pid was reused holds nothing and is never named), for | |
Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/back.py +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -779,6 +779,11 @@ def _rerun_hint(runs): | 779 | 779 | return f'W1 did not stop T3; run {again} again to finish it' | 780 | 780 |
| 781 | 781 |
| | 782 | +NO_HOLDER = ('none, but a run that stopped and disabled T3 has not ended ok (a reboot ends the kit\'s holder): the ' | | 783 | + 'home is free, and a T3 desktop app connected over SSH may start its own server on it while it may be ' | | 784 | + 'half-restored; quit or disconnect that app until the same `t3-golive back` ends ok') | | 785 | + | | 786 | + | 782 | 787 | def _status(cfg, adapter, add, notes, seen): | 783 | 788 | """The checks; returns 'up', 'restarting', 'down' or 'unknown' (the live server goes in seen['server']).""" | 784 | 789 | try: | @@ -788,16 +793,18 @@ def _status(cfg, adapter, add, notes, seen): | 788 | 793 | return 'unknown' | 789 | 794 | add('service', svc['loaded'] and svc['active'] and svc['enabled'] and check_mod._same_dir(svc['home'], cfg.home), | 790 | 795 | f'{osadapter.UNIT} {svc.get("state")}, enabled: {svc["enabled"]}, serves {svc["home"]}') | 791 | | - holder = handoff.find_holder(cfg.home) | | 796 | + holder = handoff.holder_seen(cfg.home, adapter.unit_active) | 792 | 797 | if holder is not None: # #530 | 793 | | - add('home holder', False, handoff.holder_said(cfg.home, holder)) | | 798 | + add('home holder', False, holder) | 794 | 799 | try: | 795 | 800 | runs = journal.read(cfg.state_dir)[0] | 796 | 801 | except journal.Damaged as e: # a journal from a newer kit included | 797 | 802 | runs = [] | 798 | 803 | add('journal', False, str(e)) | 799 | 804 | else: | 800 | 805 | open_run = runs[-1] if runs and journal.needs_settling(runs[-1]) else None | | 806 | + if holder is None and open_run is not None and not svc['active'] and not svc['enabled']: | | 807 | + add('home holder', False, NO_HOLDER) # a reboot ended the holder a W1 or switch left (#530) | 801 | 808 | if open_run is None: | 802 | 809 | add('journal', True, 'no golive run was left half-way') | 803 | 810 | elif open_run['command'] in FOLLOWS: | |
Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/check.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -125,9 +125,9 @@ def all_ok(): | 125 | 125 | else: | 126 | 126 | add('boot service', True, f'{osadapter.UNIT} serves this home') | 127 | 127 |
| 128 | | - holder = handoff.find_holder(cfg.home) | | 128 | + holder = handoff.holder_seen(cfg.home, adapter.unit_active) | 129 | 129 | if holder is not None: # #530: a W1 or switch left the home held on purpose | 130 | | - add('home holder', False, handoff.holder_said(cfg.home, holder)) | | 130 | + add('home holder', False, holder) | 131 | 131 |
| 132 | 132 | server = None | 133 | 133 | try: | |
Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/coldcopy.py +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -685,6 +685,11 @@ def hold_home(adapter, home, wait): | 685 | 685 | if pid is not None: | 686 | 686 | return handoff.Holder(home, pid), {'holder': pid, 'holderFrom': 'an earlier run that stopped T3'} | 687 | 687 | unit, why = handoff.holder_unit(home), None | | 688 | + if handoff.lock_holders(os.path.join(home, 'userdata', OWNER_LOCK)) is None: # none it could find again | | 689 | + return hold_owner_lock(os.path.join(home, 'userdata'), wait), { | | 690 | + 'holder': None, 'holderError': f'/proc/locks cannot be read, so {unit} was not started (the kit could ' | | 691 | + 'not find it again); this run holds the home itself: it is free once the ' | | 692 | + 'run ends'} | 688 | 693 | began = time.monotonic() | 689 | 694 | try: | 690 | 695 | adapter.run_detached(unit, [KIT_BIN, *handoff.holder_args(home)]) | |
Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/handoff.py +37-9Lines changed: 37 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 40 | 40 | has no time limit there) instead of starting its own server on that home. Whoever starts T3 next takes it over | 41 | 41 | without a gap: the module's starting() hands the holder's lock to the new launcher exactly like the kit's own, and | 42 | 42 | the next W1 adopts it at its stop step (one holder at most: a second failure starts none). `status` and `check` | 43 | | -name it, `settle` stops it, and a reboot drops it (W1 left the unit disabled: nothing starts at boot). A holder is | | 43 | +name it, `settle` stops it. A reboot drops it: a known gap, kept on purpose. W1 left the unit disabled, so nothing | | 44 | +starts T3 at boot, but an SSH launch of the desktop app after the reboot finds the home free; `status` says so then | | 45 | +(no holder, T3 stopped and disabled, a run unfinished) and names the remedy. A unit that outlives a reboot would be | | 46 | +state on disk that can outlive its reason (the bug #530 avoids by finding the holder by what it is). A holder is | 44 | 47 | found by what it is, never by a file: a process with the holder's command line for this home among the owner lock's | 45 | | -holders in /proc/locks (find_holder), so a stale pid can never be taken for one. | | 48 | +holders in /proc/locks (find_holder), so a stale pid can never be taken for one. With /proc/locks unreadable the kit | | 49 | +starts no holder (it could not find it again); a holder unit that is active then is named by its unit. | 46 | 50 |
| 47 | 51 | foreign() is only a backstop, for what the two cannot cover: an SSH client older than 0.1.10 (it reads no marker and | 48 | 52 | probes no lock), or a server started by hand. It names a server that holds the home's owner lock but was not started | @@ -202,7 +206,8 @@ def let_go(self): | 202 | 206 | def starting(self, fn, version, service): | 203 | 207 | """Run fn (it starts T3 `version`: a service start, or `t3 update`), and let go of the owner lock only once | 204 | 208 | the new launcher holds its serving lock (or HANDOVER_WAIT after fn returned, or fn failed). service: reads | 205 | | - the boot service (adapter.service). Returns fn's result. Afterwards the kit holds no lock on the home. | | 209 | + the boot service (adapter.service). Returns fn's result. Afterwards the kit holds no lock on the home; a | | 210 | + holder (#530) is left holding unless the launcher took the home or the service is active. | 206 | 211 | A handover to the launcher measures its delay (handover_ms, an upper bound: the claim came after the start of | 207 | 212 | the last look that did not find the launcher serving, and the lock was let go before the clock is read).""" | 208 | 213 | self.handover, self.handover_ms = None, None | @@ -215,8 +220,9 @@ def starting(self, fn, version, service): | 215 | 220 |
| 216 | 221 | serving = os.path.join(self.home, 'runtime', SERVING_LOCK) | 217 | 222 |
| | 223 | + looked = [time.monotonic()] # the start of the last look that did not find the launcher serving | | 224 | + | 218 | 225 | def watch(): # HANDOVER_WAIT counts from fn's return (`t3 update` installs first, then restarts T3) | 219 | | - looked = time.monotonic() # the start of the last look that did not find the launcher serving | 220 | 226 | try: | 221 | 227 | while not done.is_set(): | 222 | 228 | began = time.monotonic() | @@ -228,17 +234,20 @@ def watch(): # HANDOVER_WAIT counts from fn's return (`t3 update` installs firs | 228 | 234 | if launcher_serving(self.home, svc): | 229 | 235 | outcome['how'] = 'launcher' | 230 | 236 | self.let_go() | 231 | | - outcome['ms'] = (time.monotonic() - looked) * 1000 | | 237 | + outcome['ms'] = (time.monotonic() - looked[0]) * 1000 | 232 | 238 | return | 233 | | - looked = began | | 239 | + looked[0] = began | 234 | 240 | if returned and time.monotonic() >= returned[0] + HANDOVER_WAIT: | 235 | 241 | outcome['how'] = 'timeout' | 236 | 242 | return | 237 | 243 | done.wait(HANDOVER_POLL) | 238 | 244 | outcome['how'] = 'start failed' | 239 | 245 | finally: | 240 | | - if outcome.get('how', 'start failed') == 'start failed' and _stays(self.lock) and not _active(service): | 241 | | - self.lock = None # T3 did not come up: the holder keeps the home owned past this run (#530) | | 246 | + # T3 did not come up (fn raised, or returned without starting it: `t3 update` exited non-zero or timed | | 247 | + # out, a target that never runs): the holder keeps the home owned past this run (#530). It goes only | | 248 | + # to a launcher, or when the service runs (an old launcher that took no serving lock). | | 249 | + if outcome.get('how') != 'launcher' and _stays(self.lock) and not _active(service): | | 250 | + self.lock = None | 242 | 251 | else: | 243 | 252 | self.let_go() | 244 | 253 | watcher = threading.Thread(target=watch, name='golive-handover', daemon=True) | @@ -514,7 +523,26 @@ def holder_to_service(home, svc): | 514 | 523 | def holder_said(home, pid): | 515 | 524 | return (f'the kit\'s holder {holder_unit(home)} (pid {pid}) keeps the home owned, so no SSH launch starts a server ' | 516 | 525 | 'on it: a W1 or switch that stopped T3 and did not end ok left it (the home may be half-restored); the ' | 517 | | - 'same `t3-golive back` takes it over, or put the home right and `t3-golive settle` stops it') | | 526 | + 'same `t3-golive back` takes it over, or put the home right and `t3-golive settle` stops it; do not start ' | | 527 | + 't3code.service by hand while it holds (its server would idle)') | | 528 | + | | 529 | + | | 530 | +def holder_seen(home, unit_active): | | 531 | + """What `status` and `check` say about the holder, or None when there is none: found in /proc/locks, or, when | | 532 | + that cannot be read, its unit is active (unit_active: adapter.unit_active; it may raise).""" | | 533 | + pid = find_holder(home) | | 534 | + if pid is not None: | | 535 | + return holder_said(home, pid) | | 536 | + if lock_holders(os.path.join(home, 'userdata', OWNER_LOCK)) is not None: | | 537 | + return None | | 538 | + unit = holder_unit(home) | | 539 | + try: | | 540 | + if not unit_active(unit): | | 541 | + return None | | 542 | + except Exception: # noqa: BLE001 (cannot tell: say nothing more than the failed read) | | 543 | + return None | | 544 | + return (f'the kit\'s holder unit {unit} is active, and /proc/locks cannot be read, so the kit cannot tell what it ' | | 545 | + f'holds: it may keep the home owned; once the home is right, `systemctl --user stop {unit}`') | 518 | 546 |
| 519 | 547 |
| 520 | 548 | def serve_holder(home, wait=None): | |
Collapse file Copy file name to clipboardExpand all lines: fork-kit/golive/lib/golive/switch.py +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -764,7 +764,10 @@ def guard(cfg, adapter, run_id, wait, say=print, lose_work_since=None): | 764 | 764 | except (journal.Damaged, OSError) as e: | 765 | 765 | runs = None | 766 | 766 | did.append(f'journal unreadable, so T3 is started if it is not active: {e}') | 767 | | - if not adapter.service()['active']: | | 767 | + svc = adapter.service() | | 768 | + if not svc['active'] and not svc['enabled']: # the switch never disables it; its W1 does, on purpose | | 769 | + did.append(f'{osadapter.UNIT} is not enabled: left stopped (a W1 that stopped may hold it)') | | 770 | + elif not svc['active']: | 768 | 771 | coldcopy.open_gap(cfg.home) # left to run out: this guard does not wait for T3 | 769 | 772 | try: # the killed run's holder (#530) lets go to the launcher | 770 | 773 | handoff.starting(cfg.home, adapter.start, None, adapter.service) | |
|