Skip to content

fix: goroutine leaks in event executor and improve shutdown handling#472

Open
beeme1mr wants to merge 1 commit intomainfrom
fix-event-goroutine-leak
Open

fix: goroutine leaks in event executor and improve shutdown handling#472
beeme1mr wants to merge 1 commit intomainfrom
fix-event-goroutine-leak

Conversation

@beeme1mr
Copy link
Member

This PR

  • properly shuts down the event executor

Related Issues

Fixes #471

Notes

Added go.uber.org/goleak to try and detect other leaks.

How to test

Run test suite

…tdown handling

Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
@beeme1mr beeme1mr requested review from a team as code owners February 11, 2026 01:19
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @beeme1mr, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the stability and resource management of the OpenFeature SDK by addressing potential goroutine leaks within the event executor. It introduces a robust shutdown mechanism that ensures all background event processing routines are properly terminated when no longer needed, and integrates a powerful leak detection tool to safeguard against future concurrency issues. These changes enhance the overall reliability of the SDK, particularly in long-running applications or scenarios involving frequent provider changes.

Highlights

  • Goroutine Leak Prevention: Implemented a comprehensive shutdown mechanism for the eventExecutor to prevent goroutine leaks, utilizing sync.Once, sync.WaitGroup, and dedicated done channels for graceful termination of event processing goroutines.
  • Enhanced Shutdown Handling: Modified Shutdown() and ShutdownWithContext() functions, as well as initSingleton(), to explicitly call the new eventExecutor shutdown logic, ensuring all associated goroutines are cleaned up when the OpenFeature SDK is reset or shut down.
  • Goroutine Leak Detection: Integrated go.uber.org/goleak into the test suite and added new tests (TestNoGoroutineLeak, TestNoGoroutineLeakWithMultipleProviders, TestNoGoroutineLeakWithShutdownWithContext) to automatically detect and prevent future goroutine leaks.
  • Test Utility Refactoring: Introduced helper functions expectChannelReceive and expectTimeout in tests to simplify and standardize assertions related to channel operations, improving test readability and reliability.
  • Buffered Shutdown Semaphore: Changed the shutdownSemaphore in providerReference to a buffered channel, allowing for more robust signaling during shutdown without blocking.
Changelog
  • go.mod
    • Added go.uber.org/goleak dependency for goroutine leak detection.
  • go.sum
    • Updated checksums to include go.uber.org/goleak and removed outdated golang.org/x/sync and golang.org/x/text entries.
  • openfeature/client_test.go
    • Removed unused math import.
    • Modified TestRequirement_1_7_6 to include event executor shutdown in cleanup and use a controlled blocking channel for provider initialization testing.
  • openfeature/event_executor.go
    • Added shutdownOnce, done channel, and sync.WaitGroup to eventExecutor for controlled shutdown.
    • Initialized the done channel in newEventExecutor.
    • Updated startListeningAndShutdownOld to track goroutines with WaitGroup and listen for shutdown signals.
    • Refactored startEventListener to use WaitGroup and a select statement for graceful termination upon done channel closure.
    • Implemented a shutdown() method to signal, wait for, and drain event executor goroutines.
  • openfeature/event_executor_test.go
    • Imported go.uber.org/goleak.
    • Modified TestEventHandler_multiSubs to ensure goroutines are cleanly shut down during test cleanup.
    • Added TestBasicShutdown to verify the shutdown() method's functionality.
    • Added TestNoGoroutineLeakWithMultipleProviders to confirm no goroutine leaks occur with multiple registered providers using goleak.VerifyNone.
  • openfeature/main_test.go
    • Added TestMain to ensure global event executor shutdown after all tests complete, preventing cross-test goroutine leaks.
  • openfeature/openfeature.go
    • Modified initSingleton() to shut down any existing event executor before reinitialization.
    • Updated Shutdown() and ShutdownWithContext() to explicitly shut down the event executor before reinitializing the singleton.
  • openfeature/openfeature_test.go
    • Imported context and go.uber.org/goleak.
    • Replaced direct select statements with new expectChannelReceive and expectTimeout helper functions for channel assertions.
    • Added expectChannelReceive and expectTimeout helper functions for robust channel testing.
    • Added TestNoGoroutineLeak to verify proper goroutine cleanup after event processing and shutdown.
    • Added TestNoGoroutineLeakWithShutdownWithContext to ensure ShutdownWithContext also prevents goroutine leaks.
  • openfeature/reference.go
    • Changed shutdownSemaphore in providerReference to a buffered channel to prevent blocking during shutdown signaling.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This is an excellent pull request that addresses a critical issue of potential goroutine leaks in the event executor. The implementation of a graceful shutdown mechanism using standard concurrency primitives like sync.WaitGroup, sync.Once, and a done channel is well-executed and robust. The addition of go.uber.org/goleak and the comprehensive new tests that verify the absence of leaks are particularly commendable, as they significantly improve the reliability of the library. The refactoring of existing tests to prevent leaks and improve readability is also a great enhancement. I have one minor suggestion to make the channel draining logic in the shutdown method more idiomatic and robust.

Comment on lines +397 to +399
for len(e.eventChan) > 0 {
<-e.eventChan
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

While the current approach to draining the event channel is likely safe because all producer goroutines should have been stopped and waited for via e.wg.Wait(), using len(e.eventChan) in a loop can be racy if there are concurrent writers. A more robust and idiomatic way to drain a channel non-blockingly is to use a select with a default case. This ensures that the drain operation is safe even if future code changes inadvertently introduce a race condition.

for {
			select {
			case <-e.eventChan:
			default:
				return
			}
		}

@beeme1mr beeme1mr changed the title fix(event): prevent goroutine leaks in event executor and improve shutdown handling fix: goroutine leaks in event executor and improve shutdown handling Feb 11, 2026
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.

[BUG] Unexpected Goroutine Leak in OpenFeature Go SDK in Test Environment.

1 participant