Files
steampipe/cmd/root.go
Nathan Wallace 46809b7982 Fix #4708: Thread-safe AddCommands/ResetCommands with mutex (#4904)
* Add test demonstrating race condition in AddCommands/ResetCommands

Added TestAddCommands_Concurrent which exposes data races when
AddCommands() and ResetCommands() are called concurrently.
Running with -race flag shows multiple race condition warnings.

This test demonstrates bug #4708 where these functions are not
thread-safe. While not a practical issue (only called during
single-threaded CLI initialization), proper synchronization
should be added.

Related to #4708

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Fix race condition in AddCommands/ResetCommands with mutex

Added thread-safe synchronization to AddCommands() and created a
new thread-safe ResetCommands() wrapper using a shared mutex.

The underlying cobra.Command methods are not thread-safe, causing
data races when called concurrently. While these functions are
typically only called during single-threaded CLI initialization,
adding proper synchronization ensures correctness and allows for
safe concurrent usage in tests.

Changes:
- Added commandMutex to protect concurrent access to rootCmd
- Updated AddCommands() with mutex lock/unlock
- Created ResetCommands() wrapper with mutex protection
- Updated test to use the new thread-safe ResetCommands()

Test now passes with -race flag with no warnings.

Fixes #4708

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-11-17 04:04:22 -05:00

4.4 KiB