Skip to content

feat(abci/server): accept pre-bound listener in socket and gRPC servers - #5904

Merged
aljo242 merged 5 commits into
cometbft:mainfrom
songgaoye:song/socket_server_listener
Jun 4, 2026
Merged

aljo242 merged 5 commits into
cometbft:mainfrom
songgaoye:song/socket_server_listener

Conversation

@songgaoye

@songgaoye songgaoye commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add NewSocketServerWithListener and NewGRPCServerWithListener constructors to the ABCI socket and gRPC servers, following the same convention as net/http.Serve and grpc.Server.Serve
  • OnStart skips net.Listen when a listener is already set, so both constructors share the same start path

Motivation

The existing constructors always call net.Listen inside OnStart, which has two drawbacks:

  • TOCTOU: there is a race between selecting a port and binding it — another process can grab the port in that window. A pre-bound listener eliminates the race entirely.
  • Testability: tests must pick a fixed port upfront, risking conflicts in parallel runs. With net.Listen("tcp", "127.0.0.1:0") the OS assigns an ephemeral port, and the caller knows the exact address before the server starts.

Socket-activated processes (e.g. systemd) also rely on passing pre-bound file descriptors, which this enables.

Test plan

  • TestNewSocketServerWithListener: binds an ephemeral port, starts the server via the new constructor, dials it, stops cleanly
  • TestNewGRPCServerWithListener: same pattern for the gRPC server
  • Existing tests continue to pass

PR checklist

  • Tests written/updated
  • Changelog entry added in CHANGELOG.md
  • Updated relevant documentation (docs/ or spec/) and code comments

@songgaoye
songgaoye requested a review from a team as a code owner June 1, 2026 14:51
@greptile-apps

greptile-apps Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

PR author is not in the allowed authors list.

@aljo242 aljo242 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

clean, well-motivated change. nil-listener guard in OnStart is the right pattern — existing callers unaffected, new constructors tested for both socket and grpc.

@aljo242
aljo242 enabled auto-merge June 4, 2026 14:30
@aljo242
aljo242 added this pull request to the merge queue Jun 4, 2026
Merged via the queue into cometbft:main with commit d27fe6a Jun 4, 2026
34 checks passed
@songgaoye
songgaoye deleted the song/socket_server_listener branch June 5, 2026 13:01
kevindharmawan pushed a commit to SeeMoReLab/cometbft that referenced this pull request Jul 30, 2026
…rs (cometbft#5904)

## Summary

- Add `NewSocketServerWithListener` and `NewGRPCServerWithListener`
constructors to the ABCI socket and gRPC servers, following the same
convention as `net/http.Serve` and `grpc.Server.Serve`
- `OnStart` skips `net.Listen` when a listener is already set, so both
constructors share the same start path

## Motivation

The existing constructors always call `net.Listen` inside `OnStart`,
which has two drawbacks:

- **TOCTOU**: there is a race between selecting a port and binding it —
another process can grab the port in that window. A pre-bound listener
eliminates the race entirely.
- **Testability**: tests must pick a fixed port upfront, risking
conflicts in parallel runs. With `net.Listen("tcp", "127.0.0.1:0")` the
OS assigns an ephemeral port, and the caller knows the exact address
before the server starts.

Socket-activated processes (e.g. systemd) also rely on passing pre-bound
file descriptors, which this enables.

## Test plan

- [x] `TestNewSocketServerWithListener`: binds an ephemeral port, starts
the server via the new constructor, dials it, stops cleanly
- [x] `TestNewGRPCServerWithListener`: same pattern for the gRPC server
- [ ] Existing tests continue to pass

---

#### PR checklist

- [x] Tests written/updated
- [x] Changelog entry added in `CHANGELOG.md`
- [ ] Updated relevant documentation (`docs/` or `spec/`) and code
comments

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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.

2 participants