Skip to content

Apply crash function to Windows Console via SetConsoleCtrlHandler - #141

Open
Kodikuu wants to merge 1 commit into
stablefrom
win-console-ctrl
Open

Apply crash function to Windows Console via SetConsoleCtrlHandler#141
Kodikuu wants to merge 1 commit into
stablefrom
win-console-ctrl

Conversation

@Kodikuu

@Kodikuu Kodikuu commented Oct 10, 2024

Copy link
Copy Markdown

Needs some testing, but this should make application quits more graceful when running with an open console on Windows.

Closing the console or Ctrl+C-ing it will be treated the same as a forced termination via Task Manager
Ctrl+Break acts like a crash, because that's useful to have (Ctrl+C and Ctrl+Break events are when inputting to the console)

Shutdown will plainly exit the application since it shouldn't remain running, while logoff events will leave things down to Windows

@Kodikuu Kodikuu added the 2. Ready for Review The implementation work is done, and this can be reviewed label Oct 10, 2024
@Kodikuu Kodikuu self-assigned this Oct 10, 2024

@dvijayak dvijayak left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

See my note.

Comment thread src/windows/systemw.c
Comment on lines +235 to +240
// Console close == SIGTERM, Ctrl+C == SIGINT
if (SYSTEM_CRASH_FUNC)
SYSTEM_CRASH_FUNC(dwCtrlType == CTRL_CLOSE_EVENT || dwCtrlType == CTRL_C_EVENT, SYSTEM_OPAQUE);

// Prevent secondary calls to the SYSTEM_CRASH_FUNC
SYSTEM_CRASH_FUNC = NULL;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't know if you need to protect against multi-threaded scenarios, but perhaps you should do an atomic exchange of the crash func pointer into a local variable while setting the static variable to NULL.

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

Labels

2. Ready for Review The implementation work is done, and this can be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants