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

Fix race condition in plugin logs routine #2126

Merged
merged 2 commits into from
Jun 3, 2022

Conversation

carolynvs
Copy link
Member

What does this change

When we run a plugin, we start a go routine that captures logs from the plugin and forwards them to our traceLogger. I've found that there is a race condition where:

  1. We close the plugin
  2. The plugin emits a message saying that its closing
  3. We cancel the context for the log go routine but ctx.Done doesn't immediately trigger on the go routine.
  4. Instead the plugin connections's close function returns before the routine is stopped.
  5. Porter moves on and closes our logger.
  6. The log go routine then tries to forward the message to the logger which is now closed.

I've added a wait group that ensures that the log go routine finishes before the plugin connection returns from Close. This ensures that all resources managed by the plugin connection are cleaned up before Close returns.

What issue does it fix

If someone is running in debug mode, a good percentage of the time, there would be a debug message saying that we couldn't write to the close log file at the end of the porter command. This removes that noise and ensures that if there is an error emitted by the plugin on close that we capture it.

Notes for the reviewer

N/A

Checklist

  • Did you write tests?
  • Did you write documentation?
  • Did you change porter.yaml or a storage document record? Update the corresponding schema file.
  • If this is your first pull request, please add your name to the bottom of our Contributors list. Thank you for making Porter better! 🙇‍♀️

Reviewer Checklist

  • Comment with /azp run test-porter-release if a magefile or build script was modified
  • Comment with /azp run porter-integration if it's a non-trivial PR

When we run a plugin, we start a go routine that captures logs from the
plugin and forwards them to our traceLogger. I've found that there is a
race condition where:

1. We close the plugin
2. The plugin emits a message saying that its closing
3. We cancel the context for the log go routine but ctx.Done doesn't
   immediately trigger on the go routine.
4. Instead the plugin connections's close function returns before the routine
   is stopped.
5. Porter moves on and closes our logger.
6. The log go routine then tries to forward the message to the logger
   which is now closed.

I've added a wait group that ensures that the log go routine finishes
before the plugin connection returns from Close. This ensures that all
resources managed by the plugin connection are cleaned up before Close
returns.

Signed-off-by: Carolyn Van Slyck <[email protected]>
@@ -286,6 +293,9 @@ func (c *PluginConnection) setupLogCollector(ctx context.Context) {
// The best way to get that information is to instrument the plugin itself. This is mainly a fallback mechanism to
// collect logs from an uninstrumented plugin.
func (c *PluginConnection) collectPluginLogs(ctx context.Context) {
c.logsWaitGroup.Add(1)
Copy link
Contributor

Choose a reason for hiding this comment

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

What do u think moving this line to line 287?
That way we know where the goroutine is started

Signed-off-by: Carolyn Van Slyck <[email protected]>
@carolynvs carolynvs marked this pull request as ready for review June 3, 2022 13:30
@carolynvs
Copy link
Member Author

@VinozzZ Fixed!

Copy link
Contributor

@VinozzZ VinozzZ left a comment

Choose a reason for hiding this comment

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

Yahoo!!

@carolynvs carolynvs merged commit 23dd9bc into getporter:release/v1 Jun 3, 2022
@carolynvs carolynvs deleted the plugin-logs-race branch June 3, 2022 17:48
joshuabezaleel pushed a commit to joshuabezaleel/porter that referenced this pull request Jun 6, 2022
* Fix race condition in plugin logs routine

When we run a plugin, we start a go routine that captures logs from the
plugin and forwards them to our traceLogger. I've found that there is a
race condition where:

1. We close the plugin
2. The plugin emits a message saying that its closing
3. We cancel the context for the log go routine but ctx.Done doesn't
   immediately trigger on the go routine.
4. Instead the plugin connections's close function returns before the routine
   is stopped.
5. Porter moves on and closes our logger.
6. The log go routine then tries to forward the message to the logger
   which is now closed.

I've added a wait group that ensures that the log go routine finishes
before the plugin connection returns from Close. This ensures that all
resources managed by the plugin connection are cleaned up before Close
returns.

Signed-off-by: Carolyn Van Slyck <[email protected]>

* Move when we add to the wait group

Signed-off-by: Carolyn Van Slyck <[email protected]>
Signed-off-by: joshuabezaleel <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants