hashicorp / hashicorp/hcl

Is NewBlock save to use concurrently

Open
#428 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

I wonder why calling `NewBlock` seems to trigger race detection.

### Expected behavior
Everything should work fine.

### Actual behavior

Golang race detector complains:

```
WARNING: DATA RACE
Write at 0x0000009f7640 by goroutine 15:
github.com/hashicorp/hcl/v2/hclwrite.formatSpaces()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/format.go:127 +0x1ca
github.com/hashicorp/hcl/v2/hclwrite.format()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/format.go:36 +0xa6
github.com/hashicorp/hcl/v2/hclwrite.TokensForValue()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/generate.go:25 +0xa5
github.com/hashicorp/hcl/v2/hclwrite.(*Block).init()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/ast_block.go:39 +0x70c
github.com/hashicorp/hcl/v2/hclwrite.NewBlock()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/ast_block.go:29 +0x179
...

Previous write at 0x0000009f7640 by goroutine 14:
github.com/hashicorp/hcl/v2/hclwrite.formatSpaces()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/format.go:127 +0x1ca
github.com/hashicorp/hcl/v2/hclwrite.format()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/format.go:36 +0xa6
github.com/hashicorp/hcl/v2/hclwrite.TokensForValue()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/generate.go:25 +0xa5
github.com/hashicorp/hcl/v2/hclwrite.(*Block).init()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/ast_block.go:39 +0x70c
github.com/hashicorp/hcl/v2/hclwrite.NewBlock()
/home/abergmei/go/pkg/mod/github.com/hashicorp/hcl/v2@v2.6.0/hclwrite/ast_block.go:29 +0x179
...

```

From the code it seems like this may happen if the format lines get shared. I however could not find such a package variable yet.

### Steps to reproduce
1. Start two goroutines
2. Have functions call `hclwrite.NewBlock` in both of them

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with hclwrite.NewBlock in hclwrite/ast_block.go, then read hclwrite/generate.go and hclwrite/format.go around TokensForValue and formatSpaces. Reproduce the report with two goroutines under the Go race detector. Done means the concurrent NewBlock case no longer reports a race, with coverage added for the regression.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.