Go: gsignal, Master of Signals

Vincent Blanchon
Mar 9, 2020 · 3 min read
Illustration created for “A Journey With Go”, made from the original Go Gopher, created by Renee French.

ℹ️ This article is based on Go 1.13.

The package signal provides signal handlers and allows our Go program to interact with the incoming signals. Let’s start with the listeners before diving into the internals.

Subscription

Each os.Signal channel listens to their own set of events. Here is a diagram with the subscription workflow of the previous example:

Go also gives the ability for a channel to stop being notified — function Stop(os.Signal) — or to ignore signals — function Ignore(...os.Signal). Here is a short example of each:

This program cannot be interrupted by CTRL+C and will never stop since the channel stopped listening to the signal for the terminal resizing before receiving from the channel a second time. Let’s now see how the listener and process phases that handle the incoming signals are built.

gsignal

Then, when a signal reaches the program, the signal handler delegates it to a special goroutine called gsignal. This goroutine is created with a bigger stack (32k, in order to fulfill the requirement by the different OS) that is fixed and cannot grow. Each thread (represented by M) has an internal gsignal goroutine to handle the signals. Here is the updated diagram:

gsignal analyzes the signal to check if it processable, and wakes up the sleeping goroutine along with sending the signal to the queue:

Synchronous signals, like SIGBUS or SIGFPE, are not manageable and will be converted to panics

Then, this looping goroutine can process it. It finds first the channels that have subscribed to this event, and pushes the signal to them:

The goroutine that is looping to process signals can be visualized in the traces via the tool go tool trace:

A lock or block of gsignal would make the signal handling under troubles. It also cannot allocate memory due to its fix size. This is why it is important to have two separated goroutines in the signal processing chain: one to queue the signals as soon as they arrived, and another one to process them looping on the same queue.

We can now update the illustration of the first section with the new components:

A Journey With Go

A Journey With Go Language Programming

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store