Skip to content

fix(v3): add SingleInstance.OnSecondInstanceExit for pre-exit cleanup - #6078

Closed
4RH1T3CT0R7 wants to merge 2 commits into
wailsapp:codex/mpress-markdownfrom
4RH1T3CT0R7:fix/single-instance-exit-defers
Closed

4RH1T3CT0R7 wants to merge 2 commits into
wailsapp:codex/mpress-markdownfrom
4RH1T3CT0R7:fix/single-instance-exit-defers

Conversation

@4RH1T3CT0R7

@4RH1T3CT0R7 4RH1T3CT0R7 commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

When SingleInstance finds the lock already held, the second process ends with os.Exit inside application.New, so anything the caller deferred before New (flushing a file, releasing a lock, closing a log) never runs. The darwin and linux notify implementations also had their own os.Exit, giving three exit sites for the same path.

This adds SingleInstanceOptions.OnSecondInstanceExit, called in the second instance after the first has been notified and right before os.Exit(ExitCode), and collapses the three exit sites into one manager method. Apps that don't set the hook behave exactly as before. A subprocess test checks the hook runs after notification and the process exits with ExitCode; it skips on macOS, where notifyFirstInstance runs an AppKit loop to capture the launch URL. The Linux notify overlaps with the open #5973, so whichever lands second needs a small rebase.

Fixes #6052

Summary by CodeRabbit

  • New Features

    • Added an OnSecondInstanceExit callback for cleanup or resource handling before a second application instance exits.
    • Configurable exit codes are now applied after the second instance notifies the primary instance.
  • Documentation

    • Updated the Single Instance guide with callback behavior, exit-code handling, and guidance for flushing or releasing resources before termination.

The second instance terminated via os.Exit inside application.New, and
again inside the darwin and linux notify implementations, so nothing the
caller deferred before New ever ran. Run an optional OnSecondInstanceExit
hook after notifying the first instance, and keep a single exit site.

Fixes wailsapp#6052
@github-actions github-actions Bot added Documentation Improvements or additions to documentation v3 MacOS Linux labels Sep 5, 2026
@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (1)
  • master

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: d2c4e411-2503-4c17-b5da-734c5fc42669

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 23ddf256-a356-48db-b780-317d5f3893ec

📥 Commits

Reviewing files that changed from the base of the PR and between 035ebce and 17b8107.

📒 Files selected for processing (6)
  • docs/src/content/docs/guides/single-instance.mdx
  • v3/pkg/application/application.go
  • v3/pkg/application/single_instance.go
  • v3/pkg/application/single_instance_darwin.go
  • v3/pkg/application/single_instance_linux.go
  • v3/pkg/application/single_instance_test.go
💤 Files with no reviewable changes (1)
  • v3/pkg/application/single_instance_darwin.go

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

The single-instance exit path now uses a manager method that notifies the first instance, runs an optional callback, and exits with the configured code. Platform notification methods no longer exit directly. Tests and documentation cover the new behavior.

Changes

Single-instance exit handling

Layer / File(s) Summary
Exit callback and manager flow
v3/pkg/application/application.go, v3/pkg/application/single_instance.go
Adds OnSecondInstanceExit and centralizes notification, callback execution, error logging, and os.Exit.
Platform notification return behavior
v3/pkg/application/single_instance_darwin.go, v3/pkg/application/single_instance_linux.go
Platform notification methods return after sending the message instead of exiting directly.
Callback validation and documentation
v3/pkg/application/single_instance_test.go, docs/src/content/docs/guides/single-instance.mdx
Tests verify callback ordering, notification, exit status, and defaults. The guide documents callback usage and os.Exit behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SecondInstance
  participant singleInstanceManager
  participant FirstInstance
  SecondInstance->>singleInstanceManager: application.New detects existing instance
  singleInstanceManager->>FirstInstance: notifyFirstInstance()
  singleInstanceManager->>SecondInstance: OnSecondInstanceExit()
  SecondInstance->>SecondInstance: os.Exit(ExitCode)
Loading

Suggested reviewers: leaanthony

Merge Risk: 🟡 Moderate · up to 17b81

The exit-hook behavior is implemented and documented, but the new test code contains a duplicate type declaration that prevents the package tests from compiling. Remove the duplicate before merging.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding OnSecondInstanceExit for cleanup before second-instance termination.
Description check ✅ Passed The description explains the problem, implementation, behavior, linked issue, test coverage, and platform limitation. It omits template checklists and wails doctor output, but it is otherwise substant…
Linked Issues check ✅ Passed The changes satisfy issue #6052 by adding a pre-exit cleanup hook, preserving ExitCode behavior, notifying the first instance before the hook, and preventing the second instance from continuing to Run…
Out of Scope Changes check ✅ Passed The code, documentation, and tests directly support issue #6052. No unrelated or out-of-scope changes are evident.
Full details: Docstring Coverage

Explanation

Docstring coverage is 40.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files. (1 skipped: 1 unsupported.)

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checked the second door,
Sent news to the first instance once more.
A cleanup hook ran bright,
Then ExitCode took flight,
And tests kept watch at the core.

Comment @coderabbitai help to get the list of available commands.

@taliesin-ai
taliesin-ai changed the base branch from master to codex/mpress-markdown September 18, 2026 11:20

Copy link
Copy Markdown
Member

Ported this PR's ExitCode/OnSecondInstanceExit documentation to M-Press in 397faec and updated the corresponding existing page in all nine languages (de, fr, id, ja, ko, pt, ru, zh-cn, zh-tw).

Dependency: merged #6137 at 7b0f6dc and changed this PR's base to codex/mpress-markdown. The only code conflict was composed mechanically: Linux notify now uses the current base's dbusInterface while retaining this PR's return-to-centralized-cleanup behavior instead of calling os.Exit there. Comparison of implementation edits confirmed preservation of the PR changes with that interface-name adjustment. No Starlight sources remain; no translation sidecar state or audit settings were changed. Pushed fast-forward to the original fork branch as taliesin-ai.

Validation: M-Press 1.0.17 strict build, link/asset check, and rendered-site check passed (2,010 content pages; 2,011 HTML pages; zero site errors). Linux go test ./pkg/application -run 'Test.*(SingleInstance|SecondInstance)' -count=1 passed, including cleanup/exit and D-Bus name tests. A local Go example check verified the documented option fields/callback. Manual scoped diff review and whitespace checks passed.

Known gate: the global translation audit remains failing with 7,678 findings, matching the pinned migration baseline count; findings outside this page are unchanged. The inherited migration audit blocker is separate work and the gate remains enabled. coderabbit --plain was attempted but unsupported; the single narrow fallback review hit the shared rate limit. No usage credits were spent.

@4RH1T3CT0R7

Copy link
Copy Markdown
Contributor Author

Thanks for carrying this over to M-Press and sorting out the notify conflict.

@leaanthony
leaanthony deleted the branch wailsapp:codex/mpress-markdown September 20, 2026 08:14
@leaanthony leaanthony closed this Sep 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation Linux MacOS v3

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v3] SingleInstance: os.Exit inside application.New skips the caller's defers (silent cleanup loss)

3 participants