iotexproject / iotexproject/iotex-core

pkg/messagebatcher: options mutate the shared default writer configuration

Open Beginner friendly
#4,960 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
1.6k
Forks
382
Avg merge
4d 22h
Merged PRs (30d)
17

Description

### What version of iotex-core image (or code branch) are you using?

Current `master`.

### What operating system and processor architecture are you using?

Platform-independent. This issue is caused by shared in-process configuration state.

### What did you do? If possible, provide a recipe for reproducing the error.

`Manager.Put` initializes a new writer with:

```go
cfg := _defaultWriterConfig
for _, opt := range opts {
opt(cfg)
}
```

However, _defaultWriterConfig is a package-level pointer:

```go
var _defaultWriterConfig = &writerConfig{
expiredThreshold: 2,
msgInterval: 100 * time.Millisecond,
sizeLimit: 1000,
}
```

As a result, options such as WithSizeLimit and WithInterval mutate the shared default configuration instead of a per-writer copy.

For example, when the messages have different batch IDs:

```go
manager.Put(msgA, WithSizeLimit(2))
manager.Put(msgB)
```

the writer for msgB inherits sizeLimit == 2, even though no option was supplied.

The mutation is process-wide, so it can also affect writers created by other Manager instances. Concurrent creation of writers with different options may additionally read and write the shared configuration without synchronization, causing a data race.

### What did you expect to see?

Options supplied while creating one writer should only affect that writer. A later writer created without options should use:
```shell
expiredThreshold: 2
msgInterval: 100 * time.Millisecond
sizeLimit: 1000
```

### What did you see instead?

Options permanently modify _defaultWriterConfig, causing later writers to inherit configuration supplied for earlier writers.

Contributor guide

Open the contributing guide

Research direction

Start in pkg/messagebatcher by reading Manager.Put, writerConfig, _defaultWriterConfig, and option functions such as WithSizeLimit and WithInterval. Confirm that each new writer receives independent configuration, that an option affects only its writer, and that a later writer without options retains the documented default values.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.