<!-- XE-12916 · statechain sync server: handleSyncStream has no zero-progress exit — one request makes the handler encode empty pages until the stream deadline · testnet -->
*Markdown view of <https://bounty.xe.network/XE-12916>. The page is the record; this is the same page for reading.*

XE-12916 · Bounty

# statechain sync server: handleSyncStream has no zero-progress exit — one request makes the handler encode empty pages until the stream deadline

Closed · duplicate
—claimed Minor
web
filed 2026-11-14 14:17 UTC
by xe:352fe8fc

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Minor

Area
:   P2P / sync

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-20 19:46 UTC

Told
:   2026-11-20 19:46 UTC

### Patches

None linked.

### History

- [627173](https://bounty.xe.network/log/627173) 2026-11-20 19:46 UTC proposal.adopted
- [627172](https://bounty.xe.network/log/627172) 2026-11-20 19:46 UTC reply.sent
- [627171](https://bounty.xe.network/log/627171) 2026-11-20 19:46 UTC ticket.status new → closed
- [624671](https://bounty.xe.network/log/624671) 2026-11-20 19:45 UTC proposal.adopted
- [624668](https://bounty.xe.network/log/624668) 2026-11-20 19:45 UTC proposal.adopted
- [624667](https://bounty.xe.network/log/624667) 2026-11-20 19:45 UTC ticket.facts
- [624664](https://bounty.xe.network/log/624664) 2026-11-20 19:45 UTC proposal.adopted
- [624663](https://bounty.xe.network/log/624663) 2026-11-20 19:45 UTC ticket.facts
- [606192](https://bounty.xe.network/log/606192) 2026-11-20 14:44 UTC note.added
- [606180](https://bounty.xe.network/log/606180) 2026-11-20 14:44 UTC proposal.created
- [606179](https://bounty.xe.network/log/606179) 2026-11-20 14:44 UTC proposal.created
- [606178](https://bounty.xe.network/log/606178) 2026-11-20 14:44 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-12916) · [JSON](https://bounty.xe.network/v1/tickets/XE-12916) · [markdown](https://bounty.xe.network/XE-12916.md)

### Report

### Summary

The statechain sync server's `handleSyncStream` advances `idx` by the number of
blocks returned. If `GetBlocks` returns an empty page with no error for an index
the handler believes is in range — which happens when the node's in-memory tip
is ahead of its stored tail — the handler loops forever encoding empty
`HasMore: true` pages: no zero-progress check, no page cap, no exit of its own.
One unauthenticated sync request pins the handler (and churns the chain mutex)
for the full 60-second stream deadline.

### Reproduction

Local PoC with the real handler and real `Chain.GetBlocks` over a diverged
store: a single stream produced 679,664 empty pages (21.7 MB encoded) in
500 ms, stopped only by the test's own cancellation.

### Suggested fix

Treat a zero-length page as end-of-stream (encode a final `HasMore: false`
page and return), and/or add a hard page cap per stream.

### Thread

**xe:352fe8fc**2026-11-14 15:21 UTCweb

PoC re-supplied as a message: the attachment on the original filing did not register on this ticket (it was sent under a wrong multipart field name), so the full PoC source is pasted here verbatim.

File: statechain\_server\_loop\_test.go — tests: TestPoCStatechainServerZeroProgressLoop. Verified PASS against xeprotocol/xe @ 89a542ea51d38375299aee54c6c99102e9bfa666 before filing (see report body).

```
// PoC for Lead 1: statechain/sync.go handleSyncStream has no zero-progress exit.
// In-package test (package statechain) — copy into statechain/ as
// zz_poc_test.go inside the clone, run, then delete from the clone.
//
// It drives the REAL handleSyncStream + REAL Chain.GetBlocks with a stub
// StateChainStore whose GetStateBlockRange returns (nil, nil) — a result
// both production stores produce for an empty range (see store_probe.go).
// The chain tip claims index 5 while the store holds nothing at/after 1:
// the tip/store divergence precondition.
package statechain

import (
	"bytes"
	"errors"
	"io"
	"sync/atomic"
	"testing"
	"time"

	"github.com/libp2p/go-libp2p/core/network"
	"github.com/libp2p/go-libp2p/core/protocol"
)

type emptyStore struct{}

func (emptyStore) PutStateBlock(*Block) error                    { return nil }
func (emptyStore) GetStateBlock(uint64) (*Block, error)          { return nil, nil }
func (emptyStore) GetStateTip() (*Block, error)                  { return nil, nil }
func (emptyStore) GetStateBlockRange(uint64, uint64) ([]*Block, error) {
	return nil, nil // empty range, no error — exactly like MemStore/BadgerStore
}
func (emptyStore) StateBlockCount() (uint64, error) { return 0, nil }

type fakeStream struct {
	r      io.Reader
	writes atomic.Int64
	bytes  atomic.Int64
	stop   atomic.Bool
}

func (f *fakeStream) Read(p []byte) (int, error)  { return f.r.Read(p) }
func (f *fakeStream) Write(p []byte) (int, error) {
	if f.stop.Load() {
		return 0, errors.New("poc: stream closed by test")
	}
	f.writes.Add(1)
	f.bytes.Add(int64(len(p)))
	return len(p), nil
}
func (f *fakeStream) Close() error              { return nil }
func (f *fakeStream) CloseRead() error          { return nil }
func (f *fakeStream) CloseWrite() error         { return nil }
func (f *fakeStream) Reset() error              { return nil }
func (f *fakeStream) ResetWithError(network.StreamErrorCode) error { return nil }
func (f *fakeStream) SetDeadline(time.Time) error      { return nil }
func (f *fakeStream) SetReadDeadline(time.Time) error  { return nil }
func (f *fakeStream) SetWriteDeadline(time.Time) error { return nil }
func (f *fakeStream) ID() string                       { return "poc" }
func (f *fakeStream) Protocol() protocol.ID            { return SyncProtocol }
func (f *fakeStream) SetProtocol(protocol.ID) error    { return nil }
func (f *fakeStream) Stat() network.Stats              { return network.Stats{} }
func (f *fakeStream) Conn() network.Conn               { return nil }
func (f *fakeStream) Scope() network.StreamScope       { return nil }

func TestPoCStatechainServerZeroProgressLoop(t *testing.T) {
	chain := &Chain{
		store:   emptyStore{},
		genesis: &Block{Index: 0},
		tip:     &Block{Index: 5}, // tip ahead of an empty store: divergence precondition
	}
	fs := &fakeStream{r: bytes.NewReader([]byte("{\"tip_index\":0}\n"))}
	done := make(chan struct{})
	go func() { defer close(done); handleSyncStream(fs, chain) }()
	time.Sleep(500 * time.Millisecond)
	w := fs.writes.Load()
	fs.stop.Store(true)
	select {
	case <-done:
	case <-time.After(5 * time.Second):
		t.Fatal("handler did not exit after stream failure")
	}
	t.Logf("empty syncResponse pages encoded in 500ms: %d (%d bytes)", w, fs.bytes.Load())
	if w < 1000 {
		t.Fatalf("expected runaway encoding of empty pages, got only %d writes", w)
	}
}
```

**Joseph Denne**team2026-11-20 19:46 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the
first verifiable submission, so this one closes as a duplicate. If you believe it is a different
bug, file a new report that links https://bounty.xe.network/XE-12916 and says what differs; it is read like any other.
