codemod/mine-go-potential-concurrency-hazards
Potential concurrency hazards in Go repositories
# @codemod/mine-go-potential-concurrency-hazards
Public Insights-compatible codemod package for mining potential concurrency hazards in Go repositories.
## Installation
```bash
# Install from registry
codemod run @codemod/mine-go-potential-concurrency-hazards
# Or run locally
codemod run -w workflow.yaml
```
## Usage
This package is read-only. It does not rewrite source files.
Workflow contract:
- Workflow name: `default`
- Step name: `mine_concurrency_hazards`
- Engine: `js-ast-grep`
- Language: `go`
The codemod emits one metric:
- `potential_concurrency_hazard`
Hazard types:
- `waitgroup_add_inside_goroutine`
- `map_write_in_goroutine`
- `slice_append_in_goroutine`
- `outer_scope_variable_write`
- `loop_variable_capture`
- `multiple_channel_close`
- `shared_pointer_mutation`
- `double_checked_locking`
- `request_scoped_object_in_background_goroutine`
Cardinality keys:
- `hazard_type`
- `package_name`
- `file_path`
- `function_name`
- `variable_name`
- `variable_kind`
- `goroutine_present`
- `inside_loop`
- `inside_mutex_lock`
- `inside_atomic_operation`
- `inside_sync_once`
- `channel_name`
- `struct_type`
- `framework`
- `severity`
Detection intent:
- flags `WaitGroup.Add` calls inside goroutines
- flags map writes and slice appends from goroutines
- flags writes to outer-scope variables captured by goroutines
- flags loop variable capture in goroutines started inside loops
- flags repeated `close(...)` calls on the same channel in the same function
- flags likely shared pointer mutation patterns
- flags likely double-checked locking patterns
- flags request-scoped objects used from background goroutines
The package preserves the positioning language `Potential concurrency hazards` and intentionally does not describe these findings as `Race conditions`.
## Insights
Metric documentation:
- Metric name: `potential_concurrency_hazard`
- Workflow step name: `mine_concurrency_hazards`
- Cardinality keys: `hazard_type`, `package_name`, `file_path`, `function_name`, `variable_name`, `variable_kind`, `goroutine_present`, `inside_loop`, `inside_mutex_lock`, `inside_atomic_operation`, `inside_sync_once`, `channel_name`, `struct_type`, `framework`, `severity`
Example Insights JSSG package query:
- Source: Codemod package `@codemod/mine-go-potential-concurrency-hazards`
- Select: `Metric`
- Metric: `potential_concurrency_hazard`
- Group by: `hazard_type`, `severity`
- Dimension filters: `goroutine_present = true`
The included fixtures keep source output unchanged and, when the harness supports metric snapshots, validate emitted `metrics.json` snapshots for representative hazards.
## Development
```bash
# Test fixtures and metrics snapshots
pnpm test
# Refresh expected snapshots
pnpm run test:update
# Check TypeScript types
pnpm run check-types
# Validate the workflow
codemod workflow validate -w workflow.yaml
# Publish to registry
codemod login
codemod publish
```
## License
MIT