Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions LESSONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
55 changes: 31 additions & 24 deletions src/tmodloader_mcp/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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 <name>
-skipselect`, so its very first heartbeat is already the tokened one.
Sufficient as well as necessary: a join always starts its client with
`-player <name>`, 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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
17 changes: 2 additions & 15 deletions tests/live_race_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions tests/test_session_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<port> -player <name>`: {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.
Expand Down Expand Up @@ -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 <port> -player <name>`: {cmds}"
)


def test_a_join_waits_for_THIS_clients_heartbeat_not_the_shared_one(
monkeypatch, tmp_path
):
Expand Down
Loading