Skip to content

Agent should gracefully terminate subprocesses in Windows - need a consoleΒ #9695

@pchila

Description

@pchila

PR #7738 attempted to introduce graceful termination for elastic-agent subprocesses but it does not seem to be working (ref #7756 and #6875 (comment)).

On windows, the graceful termination of subprocesses is handled by sending a CTRL+BREAK event, using GenerateConsoleCtrlEvent function specifying the correct pid (which should really be the process group id).
In the documentation we can read:

Only those processes in the group that share the same console as the calling process receive the signal. In other words, if a process in the group creates a new console, that process does not receive the signal, nor do its descendants.

When elastic-agent is running as a windows service it does not get a console, hence it cannot signal child processes properly give that there's no console shared between the processes (in unit test everything works as the go test command has a console of its own that is inherited by subprocesses).

The goal of this issue is to fix the signaling on windows, ensuring that a console is shared between elastic-agent and its subprocesses and that it is used to signal subprocesses to shutdown.

Notes:

  • A process can have at most one console attached (so it's a global object), some synchronization may be required when calling AllocConsole, AttachConsole or FreeConsole
  • A new console may be allocated when invoking subprocess (as opposed to inheriting the caller's console) using process creation flags like CREATE_NEW_CONSOLE. Alternatively a new console can be created "on demand" using AllocConsole

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions