Ancestors

Written by Julien Riou on 2025-01-21 at 17:45

My current Go program has a bug when I cancel a context, the CPU goes wild without stopping what's supposed to be stopped. Simple context/select statements are easy but they could become a nightmare in real world applications. Last time my CPU spiked, it was caused by an infinite loop without a sleep statement.

=> More informations about this toot | More toots from jriou@hachyderm.io

Written by Julien Riou on 2025-01-21 at 20:17

I may know where the CPU cycles are spent. There's a select statement waiting for a context to be canceled and a for loop with a sleep function taking a context too. The sleep is skipped when the context is closed but the infinite loop doesn't end. I should move the for loop outside of the select and use the regular sleep function. Let's see that tomorrow 💡

=> More informations about this toot | More toots from jriou@hachyderm.io

Toot

Written by Julien Riou on 2025-01-22 at 06:07

That was it!

=> More informations about this toot | More toots from jriou@hachyderm.io

Descendants

Written by Julien Riou on 2025-01-22 at 06:08

The solution looks like this:

for {

    select {

    case <-ctx.Done():

        // cancelation work

        return

    default:

        run()

        time.Sleep(...) // remove WithContext

    }

}

=> More informations about this toot | More toots from jriou@hachyderm.io

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113870506655892610
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
261.187856 milliseconds
Gemini-to-HTML Time
1.334502 milliseconds

This content has been proxied by September (3851b).