You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
25 minutes ago
​
​
la_serva: fix memory/descriptor leaks, crashes and a DB-log deadlock Leaks that grew without bound: - Dead module connections were never removed (half-open TCP after a module power loss / network drop): every one kept 3 threads, 8 MB of socket buffers, a descriptor and a 10 ms DB poll. servaRPCHadnler now drops a connection that sends nothing for RPC_IDLE_TIMEOUT_S (60 s; la_mojo sends a status about every second), and accepted sockets get TCP keepalive (30 s idle, 3 probes of 10 s). - The client socket was only closed on an I/O error: a handler removed for any other reason (module disabled / not registered, idle) leaked its descriptor until accept() failed with EMFILE - which ended the accept thread for good. ~serverPort now shuts down and closes it; transient accept() errors (EMFILE, ENFILE, ENOMEM, ECONNABORTED, EINTR, ...) no longer stop the server. recv() == 0 (peer closed) is always a disconnect, whatever errno holds. - servaSessionManager::reopenSession stored the reopened (expired) session in the per-track map, overwriting the track's ACTIVE session: its data and open report.db leaked and it was never closed in MySQL. Reopened sessions are now kept outside the map and released by the caller (addVideo, getSessionBase). - servaVideoComposer leaked its session data, an open SQLite connection and both video queues on every composition; servaKernel leaked a composer whose compose() failed, never initialised / released the automatic composer (it ran at most once) and leaked the media info of every encoded video (processVideoFile success path). Crashes / corruption: - vector erase() with the old iterator reused (netServer client cleanup, encoder / composer pools, 4 ModBus action loops). - transportSocket::_close wrote the stop byte into the read end of the pipe (threads only stopped at the 5 s select timeout, blocking the accept thread on every disconnect) and left closed pipe numbers set (a second _close could write into another connection's socket). - Session map used without the lock from RPC, kernel and ModBus threads: now a recursive mutex held for each public operation. - Freeze on the first MySQL error with DB logging enabled: Query() logs while holding the non-recursive DB mutex and the log service wrote synchronously into the DB (same-thread and two-thread deadlock). The DB log service now queues lines (max 1000) and writes them with MySQL_Connector::TrySilentQuery (never blocks, never logs); reconnect retries per query are bounded (3) instead of looping forever while holding the mutex. The log line parser also skips the UTC offset. - basicMutex::try_lock returned true when the lock was NOT taken: an uncontended try_lock took the mutex and the caller never released it (basicEngine, serial transport, ...). - Kernel restart (web commands): the ModBus singleton was deleted but not reset (next getInstance returned freed memory), the remote client kept the deleted kernel and refused to start again, and EXIT_CODE_RELOAD_SERVICES deleted the DB object every surviving component still used. Now fixed / the DB object is kept. - Life monitoring overflowed module_id[4] for platforms with > 4 modules; registerServA overflowed a 120-byte stack buffer, closed a popen() stream with fclose and leaked the config; 9 config strings in servaBackend were allocated one byte short; video chunk length is now checked against the packet; "eased" events copied every field from itself (all 0); networkServer freed libconfig's host string. Minor: ModBus action list / sessions freed at shutdown, session-start event payload freed if the event cannot be created, the per-second "No History Auto Delete!" log line moved to DEBUG_EXTRA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
25 minutes ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #ifndef __BASICMUTEX_H_INCL__
  2. #define __BASICMUTEX_H_INCL__
  3. #ifndef __linux__
  4. #ifndef WIN_PTHREAD
  5. // #define WIN_PTHREAD
  6. #endif
  7. #endif
  8. #ifdef WIN_PTHREAD
  9. #pragma comment(lib, "pthreadVC2.lib")
  10. #endif
  11. #ifndef MUTEX_HANDLE_TYPE
  12. #if defined(__linux__) || defined(WIN_PTHREAD)
  13. // Linux impl.
  14. #include <pthread.h>
  15. #define MUTEX_HANDLE_TYPE pthread_mutex_t
  16. #define MUTEX_INIT(X) pthread_mutex_init(&X, NULL)
  17. #define MUTEX_LOCK(X) pthread_mutex_lock(&X)
  18. #define MUTEX_TRY_LOCK(X) pthread_mutex_trylock(&X)
  19. #define MUTEX_UNLOCK(X) pthread_mutex_unlock(&X)
  20. #define MUTEX_DESTROY(X) pthread_mutex_destroy(&X)
  21. #else
  22. // Windows impl.
  23. #include "Windows.h"
  24. #define MUTEX_HANDLE_TYPE HANDLE
  25. #define MUTEX_INIT(X) X = CreateMutex(NULL, FALSE, NULL)
  26. #define MUTEX_LOCK(X) WaitForSingleObject(X, INFINITE)
  27. #define MUTEX_TRY_LOCK(X) (WaitForSingleObject(X, 0) == WAIT_OBJECT_0)
  28. #define MUTEX_UNLOCK(X) ReleaseMutex(X)
  29. #define MUTEX_DESTROY(X) CloseHandle(X)
  30. #endif
  31. #endif
  32. /**
  33. * Mutex wraper
  34. *
  35. * @author Gagua
  36. */
  37. class basicMutex
  38. {
  39. private:
  40. MUTEX_HANDLE_TYPE m_sMutexHandle;
  41. public:
  42. // Constructor
  43. basicMutex();
  44. // Destructor
  45. virtual ~basicMutex();
  46. void lock(void);
  47. bool try_lock(void);
  48. void unlock(void);
  49. // Copy constructor
  50. // TODO: Uncomment the copy constructor when you need it.
  51. // basicMutex(const basicMutex& src);
  52. // Assignment operator
  53. // TODO: Uncomment the assignment operator when you need it.
  54. // basicMutex& operator=(const basicMutex& src);
  55. };
  56. // Constructor implementation
  57. inline basicMutex::basicMutex()
  58. {
  59. MUTEX_INIT(m_sMutexHandle);
  60. }
  61. // Destructor implementation
  62. inline basicMutex::~basicMutex()
  63. {
  64. MUTEX_UNLOCK(m_sMutexHandle);
  65. MUTEX_DESTROY(m_sMutexHandle);
  66. }
  67. inline void basicMutex::lock(void)
  68. {
  69. MUTEX_LOCK(m_sMutexHandle);
  70. }
  71. // true = the mutex was acquired (pthread_mutex_trylock returns 0 on success). It returned the opposite:
  72. // an uncontended try_lock() took the mutex but reported failure, so the caller never released it.
  73. inline bool basicMutex::try_lock(void)
  74. {
  75. return (MUTEX_TRY_LOCK(m_sMutexHandle) == 0);
  76. }
  77. inline void basicMutex::unlock(void)
  78. {
  79. MUTEX_UNLOCK(m_sMutexHandle);
  80. }
  81. // TODO: Uncomment the copy constructor when you need it.
  82. // inline basicMutex::basicMutex(const basicMutex& src)
  83. //{
  84. // // TODO: copy
  85. //}
  86. // TODO: Uncomment the assignment operator when you need it.
  87. // inline basicMutex& basicMutex::operator=(const basicMutex& rhs)
  88. //{
  89. // if (this == &rhs) {
  90. // return *this;
  91. // }
  92. //
  93. // // TODO: assignment
  94. //
  95. // return *this;
  96. //}
  97. #endif // __BASICMUTEX_H_INCL__