From a1079107222bb963b641e5ccf3ac848f4dae1457 Mon Sep 17 00:00:00 2001 From: Jaret Arnold <96366172+musharna@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:35:05 -0500 Subject: [PATCH 1/2] fix: start the client with -join and -player only, without -skipselect On tModLoader 1.4.5, -skipselect next to -join also loads the first local world and enters it in singleplayer. That load raced the network join; when it landed first the server booted the client with "Invalid operation at this state" and launch/join waited out the full timeout (issue 108, about 2 launches in 10 on 1.4.5.8). launch and join each built the client command from their own copy of the list. Both now call _client_join_cmd, and a test for each caller compares the whole command. tests/live_race_check.py uses the same builder. Not yet run against a live game on 1.4.4.9. --- CHANGELOG.md | 9 +++++ src/tmodloader_mcp/session.py | 55 +++++++++++++++------------ tests/live_race_check.py | 17 +-------- tests/test_session_lifecycle.py | 67 +++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 338a2ca..2dda6c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,15 @@ keeping, not because they broke a released API. ## [Unreleased] +### Fixed + +- `launch` and `join` no longer pass `-skipselect` to the client. On tModLoader + 1.4.5 that flag, next to `-join`, also loads a local world and enters it. The + local load raced the join, and when it won the server kicked the client with + "Invalid operation at this state"; the call then waited out its whole + timeout. Reported with the cause and the fix in issue 108, at about 2 + launches in 10. + ## [0.7.0] - 2026-11-13 ### Added diff --git a/src/tmodloader_mcp/session.py b/src/tmodloader_mcp/session.py index 137d620..1c9fcd1 100644 --- a/src/tmodloader_mcp/session.py +++ b/src/tmodloader_mcp/session.py @@ -1930,6 +1930,32 @@ def _player_problem(player: str) -> str | None: return None +def _client_join_cmd(cfg: Config, port: int, player: str) -> list[str]: + """The command that starts a client and joins it to the local server. + + NO `-skipselect`. Next to `-join`, tModLoader 1.4.5 reads that flag as a + second instruction: load the first world on disk and enter it in + singleplayer, while `-join` connects the same client to the server. When + the local entry lands first, the server boots the client with "Invalid + operation at this state" and the call waits out its whole timeout - about + 2 launches in 10 (issue 108, measured on 1.4.5.8). `-join` with `-player` + selects the character and connects without it. + + ONE FUNCTION because `launch` and `join` start a client the same way. Two + copies of this list is how both of them carried the flag. + """ + return [ + str(cfg.dotnet), + "tModLoader.dll", + "-join", + "127.0.0.1", + "-port", + str(port), + "-player", + player, + ] + + def _client_is_ready(cfg: Config, player: str) -> bool: """Whether THIS player's client is up, live and in a world. @@ -1949,8 +1975,9 @@ def _client_is_ready(cfg: Config, player: str) -> bool: return against somebody else's stale menu record before the joining client existed at all. - Sufficient as well as necessary: a join is always `-player - -skipselect`, so its very first heartbeat is already the tokened one. + Sufficient as well as necessary: a join always starts its client with + `-player `, so the heartbeat it writes from inside the world is the + tokened one. """ return _heartbeat_ready( cfg.artifact(artifacts_for(cfg.mod_name, player).heartbeat, server=False) @@ -2214,17 +2241,7 @@ def launch( ) if mode == "server_client": - client_cmd = [ - str(cfg.dotnet), - "tModLoader.dll", - "-join", - "127.0.0.1", - "-port", - str(port), - "-player", - player, - "-skipselect", - ] + client_cmd = _client_join_cmd(cfg, port, player) subprocess.Popen( client_cmd, cwd=str(cfg.tml_dir), @@ -2351,17 +2368,7 @@ def join( ) before = _tml_pids(cfg) - client_cmd = [ - str(cfg.dotnet), - "tModLoader.dll", - "-join", - "127.0.0.1", - "-port", - str(session.port), - "-player", - player, - "-skipselect", - ] + client_cmd = _client_join_cmd(cfg, session.port, player) # SAME OWNERSHIP RULE `launch` HAS, and for the same reason: whoever spawns # owns the process until it can hand it back. A client that starts and diff --git a/tests/live_race_check.py b/tests/live_race_check.py index 181dc85..5011578 100644 --- a/tests/live_race_check.py +++ b/tests/live_race_check.py @@ -40,7 +40,7 @@ from tmodloader_mcp import inventory from tmodloader_mcp.config import load -from tmodloader_mcp.session import _tml_pids, launch, stop +from tmodloader_mcp.session import _client_join_cmd, _tml_pids, launch, stop from tmodloader_mcp.triggers import artifacts_for_server PORT_A, PORT_B = 7810, 7811 @@ -103,20 +103,7 @@ def main() -> int: **spawn, ) time.sleep(20) # let B's server bind before its client dials it - subprocess.Popen( - [ - str(cfg.dotnet), - "tModLoader.dll", - "-join", - "127.0.0.1", - "-port", - str(PORT_B), - "-player", - PLAYER_B, - "-skipselect", - ], - **spawn, - ) + subprocess.Popen(_client_join_cmd(cfg, PORT_B, PLAYER_B), **spawn) # B's server only polls once its client is attached - an empty server # runs no update hooks. Its per-port heartbeat appearing proves both diff --git a/tests/test_session_lifecycle.py b/tests/test_session_lifecycle.py index a679fa0..12d383c 100644 --- a/tests/test_session_lifecycle.py +++ b/tests/test_session_lifecycle.py @@ -596,6 +596,56 @@ def test_a_successful_launch_keeps_its_processes(monkeypatch): assert sess.started == {4808}, "the session did not record what it started" +def _recorded_spawns(monkeypatch): + """Record every command the already-installed fake `Popen` is handed.""" + inner = session_mod.subprocess.Popen + cmds: list[list[str]] = [] + + def recording(cmd, **kwargs): + cmds.append(list(cmd)) + return inner(cmd, **kwargs) + + monkeypatch.setattr(session_mod.subprocess, "Popen", recording) + return cmds + + +def _join_cmd(cfg, port, player): + return [ + str(cfg.dotnet), + "tModLoader.dll", + "-join", + "127.0.0.1", + "-port", + str(port), + "-player", + player, + ] + + +def test_launch_starts_its_client_with_join_and_player_and_nothing_else(monkeypatch): + """ISSUE 108. `-skipselect` next to `-join` is a second instruction on + tModLoader 1.4.5: load a local world and enter it. That raced the join and + got the client kicked in about 2 launches of 10, each one costing the whole + launch timeout. The client is started with `-join` and `-player` alone. + + The whole command is compared, so the flag coming back fails here and so + does losing the address, the port or the character. + """ + _fake_launch_world(monkeypatch, existing=set(), after={4808}, ready_raises=False) + cmds = _recorded_spawns(monkeypatch) + cfg = FakeCfg(Path("/tmp")) + + session_mod.launch(cfg, "server_client", port=1, player="n43n") + + # Two spawns were really recorded: the server, then the client. + assert len(cmds) == 2, cmds + assert "-server" in cmds[0] and "-join" not in cmds[0], cmds[0] + assert cmds[1] == _join_cmd(cfg, 1, "n43n"), ( + "the launch client was not started with exactly `-join 127.0.0.1 -port " + f" -player `: {cmds[1]}" + ) + + def test_refusing_to_launch_over_a_running_game_kills_nothing(monkeypatch): """The other direction of the same worry, and why `stop()` is surgical: a developer usually has their own game open. @@ -1280,6 +1330,23 @@ def test_a_joined_client_is_waited_for_and_its_pids_recorded(monkeypatch, tmp_pa ) +def test_a_join_starts_its_client_with_join_and_player_and_nothing_else( + monkeypatch, tmp_path +): + """ISSUE 108, the second caller. `join` built the same command as `launch` + from its own copy of the list, flag included, so it is pinned separately.""" + cfg, session, _ = _joinable(monkeypatch, tmp_path, spawns={6100}, ready_for="tst2") + cmds = _recorded_spawns(monkeypatch) + + session_mod.join(cfg, session, "tst2", timeout=30.0) + + assert session.joined == ["tst2"] # the join really ran to the end + assert cmds == [_join_cmd(cfg, session.port, "tst2")], ( + "the joining client was not started with exactly `-join 127.0.0.1 " + f"-port -player `: {cmds}" + ) + + def test_a_join_waits_for_THIS_clients_heartbeat_not_the_shared_one( monkeypatch, tmp_path ): From b65a703b5baa75189c04459a423f68ae74ad854b Mon Sep 17 00:00:00 2001 From: Jaret Arnold <96366172+musharna@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:35:23 -0500 Subject: [PATCH 2/2] docs(lessons): a launch flag carried across game versions without re-measuring it --- LESSONS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/LESSONS.md b/LESSONS.md index 2fe493b..36aef2d 100644 --- a/LESSONS.md +++ b/LESSONS.md @@ -5,3 +5,4 @@ - 2026-11-10: the restore latch kept the old undo after a failed restore because the RECORD said the save was half-restored, without reading the save; play in between was overwritten with no copy. Class: a safety decision taken from a state record when the bytes it describes can change after it was written. Caught now by `tests/test_saves.py` (play after an injected failed restore; restoring the undo over a played save), which fail on the old code; not lintable, so no hook. - 2026-11-10: `save_snapshot`/`save_restore` copied the configured world while `launch(world=...)` drove another, so a restore "succeeded" without touching the world the run changed. Class: an argument that overrides a setting in one tool and is never seen by the tools that act on the same thing later. Caught now by `tests/test_server_surface.py::test_the_save_verbs_follow_the_world_launch_loaded` and the world tests in `tests/test_saves.py` (mutants killed with `mutate-run`); not lintable, so no hook. - 2026-11-10: the reserved snapshot label was compared case-sensitively on a save directory that lives on NTFS, and the restore record's own file name was a legal label. Class: a name check that does not use the comparison rule of the filesystem the name lands on. Caught now by `tests/test_saves.py` (reserved names in any casing; restoring the undo by another casing), which fail on the old code; not lintable, so no hook. +- 2026-11-13: `launch` and `join` passed `-skipselect` next to `-join`; on tModLoader 1.4.5 the flag also loads a local world, the load raced the join, and about 2 launches in 10 were kicked and waited out the full timeout (issue 108, found by a user). Class: a launch flag carried over without re-measuring what it does on the next game version, and the same argument list written twice so both callers carried it. Mechanism: one `_client_join_cmd` for both callers, with a test per caller that compares the whole command (seen to fail with the flag put back).