Skip to content

walletapi: data races on package globals (Connected, daemon_height, daemon_topoheight, simulator) #34

Description

@Dirtybird99

Summary

Several package-level globals in walletapi are written and read from multiple goroutines with no synchronization:

var Connected bool = false
var daemon_height int64
var daemon_topoheight int64

  • Connected (bool) — written by Connect, test_connectivity, Keep_Connectivity; read by IsDaemonOnlineCached and consumers.
  • daemon_height / daemon_topoheight (int64) — written by test_connectivity (notification goroutine) and GetEncryptedBalanceAtTopoHeight (sync loop / RPC handler goroutines); read by Get_Daemon_Height, transfer building (daemon_topoheight at walletapi/wallet_transfer.go:238), history sync, and event dispatch.
  • simulator (bool, line 76) — written by test_connectivity, read during fee construction.

Impact

These are data races under the Go memory model — go run -race / go test -race flags them immediately. Practical symptoms are stale or torn reads on 32-bit platforms and reordering surprises (e.g. transfer building reading a daemon_topoheight that's inconsistent with the balance snapshot it pairs with). It also blocks ever running the race detector cleanly in CI, which hides new races (see the discussion on PR #26).

Fix

Short term: switch to sync/atomic types (atomic.Int64, atomic.Bool) or guard with a small mutex.

Long term: the file's own comment (line 74: "there should be no global variables, so multiple wallets can run at the same time") already points the way — move connection state into a client struct so multiple wallets/daemon connections can coexist in one process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions