Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

100% CPU usage #103

Open
pwlb opened this issue Jul 15, 2022 · 5 comments
Open

100% CPU usage #103

pwlb opened this issue Jul 15, 2022 · 5 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@pwlb
Copy link

pwlb commented Jul 15, 2022

Hello

package main

import (
	"context"
	"github.com/rabbitmq/amqp091-go"
	"sync"
	"time"
)

var wg sync.WaitGroup

func amqp(ctx context.Context) {
	defer func() {
		wg.Done()
	}()
	c, err := amqp091.Dial("amqp://guest:guest@127.0.0.1:5672/")
	if err != nil {
		panic("connection error")
	}
	defer c.Close()

	<-ctx.Done()
}

const n = 16

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	wg.Add(n)
	for i := 0; i < n; i++ {
		go amqp(ctx)
	}

	cancel()
	wg.Wait()

	time.Sleep(time.Hour)
}

The above code causes 100% CPU usage after a short while
The problem does not always occur, so you may have to run it several times to reproduce the problem
Strace attached after the problem occurred:

strace: Process 3167635 attached
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------
100,00    4,571892     4571892         1         1 futex
  0,00    0,000010           5         2           rt_sigprocmask
  0,00    0,000002           1         2           getpid
  0,00    0,000001           0         2           gettid
  0,00    0,000001           0         2           tgkill
  0,00    0,000000           0         1           rt_sigaction
------ ----------- ----------- --------- --------- ----------------
100.00    4,571906                    10         1 total

@lukebakken
Copy link
Contributor

Hi, we could use a bit more information -

  • How did you discover this issue?
  • Does it affect real-world use of this library? This is a pretty artificial scenario.
  • Do you have time to investigate it yourself?

@lukebakken lukebakken self-assigned this Jul 16, 2022
@lukebakken lukebakken modified the milestone: 1.4.0 Jul 16, 2022
@pwlb
Copy link
Author

pwlb commented Jul 20, 2022

While developing an application that has long sleeps in it, I encountered this problem. I eliminated all the external libs one by one, until the connection to rabbit alone remained, and the problem continued.

I wanted to write the simplest and shortest possible code, to reproduce the problem, and it turned out that just opening and closing the connection was enough. You can add opening a channel, publishing a message and closing a channel, and the problem will continue to occur, so I would not call it an "artificial scenario"

Unfortunately, I do not have time to look for the cause myself

@lukebakken
Copy link
Contributor

so I would not call it an "artificial scenario"

Now that we have an explanation it's not so artificial 😉

@lukebakken
Copy link
Contributor

@lukebakken
Copy link
Contributor

The problem stems from the goroutine used for a connection exiting before the connection is established. We don't have code (yet) to take into account scenarios where a connection is abruptly closed.

A workaround is to always give the connection time to establish before attempting to cancel it -
https://github.com/lukebakken/amqp091-go_gh-103/blob/main/main.go#L18-L19

@lukebakken lukebakken added bug Something isn't working help wanted Extra attention is needed labels Jul 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants