Threads and errors
Which thread calls your plugin, formats, fixes and merger, why they must be stateless, and what Settings Saver does when any of them throws.
Your plugin runs once on the launch thread. Everything else you hand to Settings Saver is called later, from several threads. Nothing you throw can break the game: Settings Saver logs it and carries on.
Who is called from where#
| Your code | Called from |
|---|---|
SettingsSaverPlugin.register |
the thread that launches the game, once |
SettingsFileFormat.read and write |
whichever thread is syncing |
SettingsFix.upgrade and downgrade |
whichever thread is syncing |
SettingsMerger.merge |
whichever thread is saving, while the profile is locked |
"Whichever thread is syncing" is one of:
- Settings Saver's background watcher,
- the menu's Save and Load actions,
- the shutdown hook, when the game closes.
What that requires#
| Code | Rule |
|---|---|
| Formats | Stateless. One instance is shared by every file it was registered for. |
| Fixes | No side effects: no I/O, no state, no Minecraft classes. The same input always gives the same output. A fix may run many times per sync. |
| Mergers | Thread-safe, stateless, quick, no I/O. |
| All of them | Never touch Minecraft classes. They may run before the game is set up, or while it shuts down. |
ModSettings instances are immutable and thread-safe. A ModSettings.Builder is not: use one from a
single thread. SettingsSaverMods.register and the registrar are thread-safe.
What happens when your code throws#
| What throws | What Settings Saver does | What still works |
|---|---|---|
a builder method (IllegalArgumentException) |
nothing: it throws in your code, inside register |
see the next row |
SettingsSaverPlugin.register |
catches and logs it; nothing of your mod is registered from that call | the game, Settings Saver, every other mod |
| the registrar refuses a registration | logs a warning and ignores the whole registration; it never throws | everything else |
SettingsFileFormat.read (IOException) |
logs it and skips that file until it can be read | your other files |
SettingsFileFormat.write (IOException) |
keeps the old file unchanged | your other files |
SettingsFix.upgrade or downgrade (RuntimeException) |
logs it and skips that fix for this conversion | the other fixes |
SettingsMerger.merge (RuntimeException) |
logs it and uses the default merge for that call | no change is lost |
Two things follow:
- Builder mistakes are loud, registration conflicts are quiet. A bad path or id throws at once with a message that says what is wrong. A path or id that another mod already took is only logged, because it depends on what else is installed.
- Don't use exceptions as control flow. Throwing from
registerdoesn't stop the game or tell the player anything. It only turns your integration off.
Writes are atomic: a format writes into a temporary file that replaces the real one only when it is complete. A format that throws halfway leaves the old file intact, and the game never sees a partly written file.
Related#
- Fix an integration that doesn't sync: the exact log lines.
- When plugins run