Skip to content

segmentation violation - nil pointer dereference #766

@0xgouda

Description

@0xgouda

Describe the bug
pgwatch crashes with segmentation violation - nil pointer dereference error

testing pgwatch locally without the docker image, monitoring the same db 20+ times to emulate a load and then this crash sometimes (not always) happen

To Reproduce
Steps to reproduce the behavior:

  1. go run ./cmd/pgwatch --sources=postgres://postgres:postgres@localhost:5432/postgres --sink=jsonfile://file.json
  2. update the webui to monitor the same db 20+ times
  3. wait

Expected behavior

Screenshots

2025-05-22 16:13:27.352 [INFO] [sink:jsonfile] [filename:file.json] [rows:1] [elapsed:47.805µs] measurements written
2025-05-22 16:13:27.465 [ERROR] [database:bookly_db] [err:failed to connect to `user=postgres database=bookly_db`:
        127.0.0.1:5432 (localhost): tls error: context canceled
        127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: operation was canceled] [host:localhost] [port:5432] [time:701.165µs] Connect
2025-05-22 16:13:27.465 [ERROR] [source:books3] [metric:db_stats] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.465 [ERROR] [source:books3] [metric:db_stats] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.466 [ERROR] [source:books3] [metric:wal] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.466 [ERROR] [source:books3] [metric:wal] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.466 [ERROR] [database:bookly_db] [err:failed to connect to `user=postgres database=bookly_db`:
        127.0.0.1:5432 (localhost): failed to write startup message: context canceled
        127.0.0.1:5432 (localhost): dial error: dial tcp 127.0.0.1:5432: operation was canceled] [host:localhost] [port:5432] [time:1.602557ms] Connect
2025-05-22 16:13:27.466 [ERROR] [source:books4] [metric:wal] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.466 [ERROR] [source:books4] [metric:wal] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.466 [INFO] [sources:17] sources refreshed
2025-05-22 16:13:27.467 [ERROR] [source:books5] [metric:db_stats] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.467 [ERROR] [source:books5] [metric:db_stats] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.468 [ERROR] [source:books4] [metric:db_stats] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.468 [ERROR] [source:books4] [metric:db_stats] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.472 [ERROR] [source:books11] [metric:wal] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.472 [ERROR] [source:books11] [metric:wal] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.472 [ERROR] [source:books5] [metric:wal] [error:closed pool] failed to fetch metrics
2025-05-22 16:13:27.472 [ERROR] [source:books5] [metric:wal] [count:1] [error:closed pool] failed to fetch metric data
2025-05-22 16:13:27.476 [INFO] [metrics:74] [presets:16] metrics and presets refreshed
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0xd0fa32]

goroutine 3363 [running]:
github.com/cybertec-postgresql/pgwatch/v3/internal/reaper.QueryMeasurements({0x1722738, 0xc0000feff0}, {0xc000691988, 0x7}, {0xc00097c500, 0x4ac}, {0x0, 0x0, 0x0})
        /home/ahmed/pgwatch/internal/reaper/database.go:33 +0x1f2
github.com/cybertec-postgresql/pgwatch/v3/internal/reaper.(*Reaper).FetchMetric(0xc0002bc7e0, {0x1722738, 0xc0000feff0}, 0xc0003a8248, {0xc00049e910, 0x8}, 0xc000819f48)
        /home/ahmed/pgwatch/internal/reaper/reaper.go:478 +0x92d
github.com/cybertec-postgresql/pgwatch/v3/internal/reaper.(*Reaper).reapMetricMeasurements(0xc0002bc7e0, {0x1722738, 0xc0000feff0}, 0xc0003a8248, {0xc00049e910, 0x8})
        /home/ahmed/pgwatch/internal/reaper/reaper.go:310 +0x919
created by github.com/cybertec-postgresql/pgwatch/v3/internal/reaper.(*Reaper).Reap in goroutine 1
        /home/ahmed/pgwatch/internal/reaper/reaper.go:162 +0x1645
exit status 2

Desktop (please complete the following information):

  • OS: Linux Mint 22
  • Browser [e.g. chrome, safari]
  • Version : direct clone of the latest codebase

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
I think the error is due to the Conn object becomes somehow nil in internal/reaper/database.go:33
if tx, err = conn.Begin(ctx); err != nil {

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions