Skip to content

Feature: logging and exitcodes styfry sync (#124)#193

Merged
hoytech merged 2 commits intohoytech:masterfrom
archief2910:feature/124-logging-and-exitcodes-styfry-sync
Apr 15, 2026
Merged

Feature: logging and exitcodes styfry sync (#124)#193
hoytech merged 2 commits intohoytech:masterfrom
archief2910:feature/124-logging-and-exitcodes-styfry-sync

Conversation

@archief2910
Copy link
Copy Markdown
Contributor

Description

This PR addresses three issues reported in #124 related to the strfry sync command hanging indefinitely, oversized event content leaking into logs, and unhelpful error handling when connecting to relays that do not support negentropy.

Changes included:

Inactivity Timeout for strfry sync:

  • Added --timeout=<seconds> CLI flag to cmd_sync.cpp. When set, the sync process aborts with exit code 1 if no WebSocket activity is observed for the specified duration.
  • Implemented using hoytech::timer (the same pattern used by RelayCron and cmd_router) with a 1-second tick and std::atomic<uint64_t> lastActivity updated on every onConnect and onMessage event.
  • Timeout fires via ws.trigger()ws.onTrigger to ensure the exit runs on the WebSocket thread, consistent with the existing cmd_stream.cpp trigger pattern.
  • Defaults to 0 (disabled) for full backward compatibility.

NOTICE Error Detection:

  • Added a NOTICE message handler in cmd_sync.cpp. If a NOTICE is received before any NEG-MSG response (tracked by bool receivedNegMsg), and the notice text matches common error keywords (case-insensitive), the sync exits with code 1 and a clear log message indicating the relay likely does not support negentropy.
  • This directly addresses the observed behavior where relays responding with "ERROR: negentropy disabled", "bad message type", "Command unrecognized", etc. caused the sync process to hang indefinitely.
  • Non-error NOTICEs and NOTICEs received after a successful NEG-MSG exchange are logged as warnings and do not abort the sync.

Remove Event Content from Rejection Logs (WriterPipeline.h):

  • Previously, rejected events were logged with up to 200 characters of their raw JSON content, creating a potential DoS vector where an attacker could send oversized events to fill relay disks with log data.
  • Now only the event's hex ID and rejection reason are logged: Rejected event <id>: <reason>. Falls back to "unknown" if the ID cannot be parsed.
  • This change affects all consumers of WriterPipeline (sync, import, router).

Truncate Large Messages in Error Paths:

  • Unexpected messages and catch-all error log lines in cmd_sync.cpp now truncate the raw message string to 512 characters, preventing unbounded log growth from malformed or adversarial relay messages.

Related Issue

Closes #124

Motivation and Context

Users running strfry sync in automated pipelines against many relays encountered processes hanging indefinitely when relays did not support the negentropy protocol (NIP-77). These relays send a human-readable NOTICE instead of a NEG-ERR, leaving strfry sync waiting forever with no way to detect the failure. The --timeout flag and NOTICE detection together ensure the process always terminates with a meaningful exit code. Additionally, the rejection logging change eliminates a disk-fill vector that could be triggered by any client sending oversized events to a relay running sync or import.

How Has This Been Tested?

Functionally tested against live Nostr relays in WSL Ubuntu:

  • --timeout flag verified present in --help output and confirmed it does not fire during an active sync against a responsive relay (relay.damus.io).
  • NOTICE detection verified against nos.lol and nostr.mom (both respond with "ERROR: bad msg: negentropy disabled"): sync exits with code 1 within ~1 second instead of hanging.
  • Rejection logging verified: large events rejected during a sync against relay.damus.io produce log lines of the form Rejected event <hex_id>: event too large: <size> with no event content.
  • Full project builds cleanly with make.

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly (CHANGES file).
  • All new and existing tests passed.

@archief2910 archief2910 changed the title Feature: logging and exitcodes styfry sync (124) Feature: logging and exitcodes styfry sync (#124) Apr 15, 2026
@hoytech
Copy link
Copy Markdown
Owner

hoytech commented Apr 15, 2026

Thanks!

@hoytech hoytech merged commit aab2b51 into hoytech:master Apr 15, 2026
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.

Considerations for logging and exit codes for styfry sync

2 participants