Commit Graph
14 Commits
Author SHA1 Message Date
Isaac ConnorandClaude Opus 5.5 894c0a5189 fix: keep the crash report signal-safe across rotation, threads and syslog refs #5215
Review follow-ups to the async-signal-safe die handler:

- The log file descriptor the handler wrote to could be closed by a log
  rotation after the handler loaded it, and its number reused by an
  unrelated file or socket. zmSetCrashLogFile() now keeps one descriptor
  that is never closed and dup2()s each new log file onto it, so it only
  ever refers to a log file.
- Lines were formatted with snprintf(), which is not async-signal-safe. A
  small fixed-buffer CrashLine appends strings, decimals and hex instead.
- glibc backtrace() loads libgcc_s on first use, which allocates. It is now
  called once when the handler is installed, and the comment that called it
  safe is corrected.
- zmdc points daemons' stderr at /dev/null, so with file logging off the
  report went nowhere. The handler now also sends each line to syslog over
  its own datagram socket, connected by zmSetCrashSyslog() when the Logger
  opens syslog, without calling syslog(3).
- A second signal arriving during the report was blocked when re-raised, so
  the process exited 255 instead of dying of it. Both paths now go through
  die_of(), which unblocks the signal.
- The shared volatile flag did not stop two threads that fault together from
  both reporting. A lock-free atomic_flag picks the reporting thread; a fault
  on another thread waits for it to end the process, and a fault inside the
  handler on the same thread dies at once.
- sigaltstack() only covers the calling thread. zmSetThreadAltStack() gives
  a thread its own stack, freed when it exits; main, the decoder and the
  analysis threads call it.

Tests fork a child per case: report formatting, the rotated log file, the
syslog datagram, a second signal mid-report, a fault on another thread
mid-report, and a stack overflow on a worker thread.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 23:29:29 -05:00
Isaac ConnorandClaude Opus 4.7 5153dc68ee fix: flush decoder_queue on decoder thread exit to avoid stale latency offset across reconnect
The decoder thread holds a raw AVCodecContext* obtained from
camera->getVideoCodecContext() and pushes packet locks into Monitor's
decoder_queue for each send_packet() that hasn't yet been matched by a
receive_frame(). Monitor::PrimeCapture() used to call camera->PrimeCapture()
(which Close()s the camera and frees the codec context) without first
stopping the decoder thread. Two problems followed:

1. Use-after-free race between the decoder thread and the camera teardown.
2. The stale decoder_queue entries survived the reconnect. The new codec
   context produced frames in send-order, so we popped the oldest stale
   entries to attribute frames that actually came from packets sent later.
   The net effect was a permanent N-packet offset between capture and
   decode (~92 packets observed in the field). Analysis blocks on
   !packet->decoded for those packets, so the packetqueue saturates at
   max_video_packet_count and stays there, spamming the "max video packets
   in the queue" warning forever.

Fix:
- Stop+Join the decoder in Monitor::PrimeCapture() before tearing down the
  codec context.
- Add Monitor::flushDecoderQueue() which marks in-flight packets decoded,
  notifies waiters, and clears the queue.
- Call it at the end of DecoderThread::Run() so any Stop()+Join() (including
  the existing one in Pause()) naturally releases stale entries.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 07:38:58 -04:00
Isaac Connor ae42c3c94d Wait on the packetqueue condition instead of sleeping. Should help analysis keep up with decoding better and offer faster shutdown. 2026-02-04 20:44:17 -05:00
Isaac ConnorandClaude Opus 4.5 df6c4a4a13 fix: address multiple bugs across core source files
- zm_analysis_thread.cpp, zm_decoder_thread.cpp: Fix potential deadlock
  in Start() by calling Stop() before joining thread
- zm_camera.cpp: Add null check after avformat_alloc_context()
- zm_comms.cpp: Fix memory leak in InetSocket::bind(), fix error message
  typo in deleteReader(), fix clearReaders/clearWriters to recalculate
  mMaxFd properly
- zm_config.cpp: Fix potential buffer underrun when parsing config files,
  fix misplaced SERVER_ID check logic
- zm_db.cpp: Fix logger level not restored on early return in zmDbDo(),
  fix empty string access in DB_HOST parsing
- zm_event.cpp: Fix typo "foudn" -> "found", fix memory leak with Tag
  allocation, fix variable shadowing with video_file
- zm_event_tag.cpp: Fix null dereference when AssignedBy is NULL
- zm_eventstream.cpp: Fix dangling pointer bugs with emplace_back
  (use auto& instead of auto), fix memory leak in loadInitialEventData

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-03 10:18:49 -05:00
Isaac Connor f131f78d34 Add Join() function 2024-10-26 14:29:40 -04:00
Isaac Connor 0d03266455 Don't join in Stop methods. THe decode and analysis thread may never get woken up from packetqueue. Leave it for the destructor. 2024-10-26 13:33:26 -04:00
Aaron Kling c4683d90a9 Format code using astyle google format
Commands used:
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.cpp
astyle --style=google --indent=spaces=2 --keep-one-line-blocks src/*.h
2024-03-26 13:43:58 -05:00
Isaac Connor 3c8b590925 Make DecoderThread::Stop wait for the thread to stop 2022-06-17 17:23:33 -04:00
Isaac Connor bd711c5230 Sleep if Decode fails instead of spinning 2022-06-17 16:32:05 -04:00
Isaac Connor 1d034b16dd Must join the previous thread before starting a new one or we crash 2021-04-17 12:50:26 -04:00
Isaac Connor 2d7479cb59 reintroduce Start() methods to analysis and decoding thread 2021-04-07 20:36:38 -04:00
Peter Keresztes Schmidt 817da4e621 Monitor: Make decoder a unique_ptr
The DecoderThread object is owned by the Monitor. Signal this by using unique_ptr.
2021-04-06 12:50:05 +02:00
Isaac Connor c39ec5873b don't include zm_utils in decoder_thread 2021-03-17 12:50:13 -04:00
Isaac Connor 6a11b23aaf Add decoder thread 2021-03-15 15:08:59 -04:00