Skip to content

plugins: Run deinit() on exit() of micro - #4231

Open
JoeKar wants to merge 2 commits into
micro-editor:masterfrom
JoeKar:feature/plugin-deinit
Open

JoeKar wants to merge 2 commits into
micro-editor:masterfrom
JoeKar:feature/plugin-deinit

Conversation

@JoeKar

@JoeKar JoeKar commented Sep 17, 2026

Copy link
Copy Markdown
Member

This came up with #4230 and allows plugins to do some finalization stuff.

The screen is usually closed with `exit()`.
This was overseen in commit e15bb88 (micro-editor#3273).
This allows plugins to do some cleanup short before micro is exited.
Comment thread cmd/micro/micro.go
Comment on lines 393 to 408
defer func() {
if err := recover(); err != nil {
if screen.Screen != nil {
screen.Screen.Fini()
}
if e, ok := err.(*lua.ApiError); ok {
fmt.Println("Lua API error:", e)
} else {
fmt.Println("Micro encountered an error:", errors.Wrap(err, 2).ErrorStack(), "\nIf you can reproduce this error, please report it at https://github.com/micro-editor/micro/issues")
}
// immediately backup all buffers with unsaved changes
for _, b := range buffer.OpenBuffers {
if b.Modified() {
b.Backup()
}
}
exit(1)
}
}()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it seems that this isn't needed any longer. Maybe just the forced backup on exit(1). 🤔
It was introduced with 6a27593 and moved with 65745a6, while the SIGTERM wasn't properly handled.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean here. This recover() is for catching panics when micro crashes, right? How else would we catch them?

@dmaluka

dmaluka commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

The idea seems reasonable. Although I'm a bit concerned that there might be some plugins that are using deinit() for doing some stuff which they are not expecting to be done on exit...

Trying to find out what was the original use case for this deinit() callback... This callback was introduced in commit 4027081 to support the built-in linter plugin (added in the same commit), as if the linter plugin used deinit() (it didn't, and it still doesn't). Splendid.

Possibly deinit() has never been actually used by anyone, ever. https://github.com/micro-editor/micro/issues?q=deinit doesn't give any insight about any actual users either.

@achadwick

Copy link
Copy Markdown

If current behaviour needs to be maintained, then a new at-exit cleanup hook plus documentation of exactly what fires when would seem appropriate.

But, if there are really no plugins out there that have one, go for it. It isn't unreasonable for plugins to receive deinit() as the editor exits, especially if they already init() at startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants