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.

509 lines
11 KiB

4 years ago
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
4 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years 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
22 hours 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
22 hours ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
​
​
7 years ago
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
4 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years 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
22 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years 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
22 hours 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
22 hours ago
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
6 years ago
​
​
7 years ago
​
7 years 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
22 hours 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
22 hours ago
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
  1. #include "mysql_class.h"
  2. #define LOG(X) m_pLog->Log("MYDB", this, X)
  3. #define LOG_ERROR(X) m_pLog->Log("MYDB", this, _LOG_ERROR, X)
  4. /* Default Constructor */
  5. MySQL_Connector::MySQL_Connector() :
  6. m_mAccessMutex()
  7. {
  8. m_pLog = basicLogCollection::getInstance();
  9. m_pDBLink = NULL;
  10. m_pDBName = NULL;
  11. m_pDBHost = NULL;
  12. m_pDBUser = NULL;
  13. m_pDBPassword = NULL;
  14. m_nPort = 0;
  15. m_bIsConnected = false;
  16. }
  17. MySQL_Connector::MySQL_Connector(CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port) :
  18. m_mAccessMutex()
  19. {
  20. m_pLog = basicLogCollection::getInstance();
  21. m_pDBLink = mysql_init(NULL);
  22. m_pDBName = NULL;
  23. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  24. strcpy(m_pDBHost, DBHost);
  25. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  26. strcpy(m_pDBUser, DBUser);
  27. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  28. strcpy(m_pDBPassword, DBPassword);
  29. m_nPort = Port;
  30. m_bIsConnected = false;
  31. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  32. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  33. {
  34. LOG_ERROR(mysql_error(m_pDBLink));
  35. }
  36. else
  37. {
  38. m_bIsConnected = true;
  39. }
  40. }
  41. MySQL_Connector::MySQL_Connector(CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port) :
  42. m_mAccessMutex()
  43. {
  44. m_pLog = basicLogCollection::getInstance();
  45. m_pDBLink = mysql_init(NULL);
  46. m_pDBName = (CSTR) malloc(strlen((const char *) DBName) + 1);
  47. strcpy(m_pDBName, DBName);
  48. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  49. strcpy(m_pDBHost, DBHost);
  50. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  51. strcpy(m_pDBUser, DBUser);
  52. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  53. strcpy(m_pDBPassword, DBPassword);
  54. m_nPort = Port;
  55. m_bIsConnected = false;
  56. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  57. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  58. {
  59. LOG_ERROR(mysql_error(m_pDBLink));
  60. }
  61. else
  62. {
  63. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  64. {
  65. m_bIsConnected = true;
  66. #ifdef DEBUG
  67. char temp[256];
  68. sprintf(temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  69. LOG(temp);
  70. #endif
  71. }
  72. else
  73. {
  74. LOG_ERROR(mysql_error(m_pDBLink));
  75. };
  76. };
  77. }
  78. /* default Destructor */
  79. MySQL_Connector::~MySQL_Connector()
  80. {
  81. if (m_pDBName != NULL)
  82. {
  83. delete(m_pDBName);
  84. }
  85. if (m_pDBHost != NULL)
  86. {
  87. delete(m_pDBHost);
  88. }
  89. if (m_pDBUser != NULL)
  90. {
  91. delete(m_pDBUser);
  92. }
  93. if (m_pDBPassword != NULL)
  94. {
  95. delete(m_pDBPassword);
  96. }
  97. if (m_pDBLink != NULL)
  98. {
  99. mysql_close(m_pDBLink);
  100. }
  101. mysql_library_end();
  102. m_bIsConnected = false;
  103. }
  104. bool MySQL_Connector::Connect(CSTR DBHost, CSTR DBUser, CSTR DBPassword, uint16_t Port)
  105. {
  106. m_bIsConnected = false;
  107. if (m_pDBLink != NULL)
  108. {
  109. mysql_close(m_pDBLink);
  110. };
  111. m_pDBLink = mysql_init(NULL);
  112. if (m_pDBName != NULL)
  113. {
  114. delete(m_pDBName);
  115. }
  116. if (m_pDBHost != NULL)
  117. {
  118. delete(m_pDBHost);
  119. }
  120. if (m_pDBUser != NULL)
  121. {
  122. delete(m_pDBUser);
  123. }
  124. if (m_pDBPassword != NULL)
  125. {
  126. delete(m_pDBPassword);
  127. }
  128. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  129. strcpy(m_pDBHost, DBHost);
  130. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  131. strcpy(m_pDBUser, DBUser);
  132. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  133. strcpy(m_pDBPassword, DBPassword);
  134. m_nPort = Port;
  135. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  136. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  137. {
  138. LOG_ERROR(mysql_error(m_pDBLink));
  139. }
  140. else
  141. {
  142. m_bIsConnected = true;
  143. }
  144. return m_bIsConnected;
  145. }
  146. bool MySQL_Connector::Connect(CSTR DBName, CSTR DBHost, CSTR DBUser, CSTR DBPassword, U16 Port)
  147. {
  148. m_bIsConnected = false;
  149. if (m_pDBLink != NULL)
  150. {
  151. mysql_close(m_pDBLink);
  152. };
  153. m_pDBLink = mysql_init(NULL);
  154. if (m_pDBName != NULL)
  155. {
  156. delete(m_pDBName);
  157. }
  158. if (m_pDBHost != NULL)
  159. {
  160. delete(m_pDBHost);
  161. }
  162. if (m_pDBUser != NULL)
  163. {
  164. delete(m_pDBUser);
  165. }
  166. if (m_pDBPassword != NULL)
  167. {
  168. delete(m_pDBPassword);
  169. }
  170. m_pDBName = (CSTR) malloc(strlen((const char *) DBName) + 1);
  171. strcpy(m_pDBName, DBName);
  172. m_pDBHost = (CSTR) malloc(strlen((const char *) DBHost) + 1);
  173. strcpy(m_pDBHost, DBHost);
  174. m_pDBUser = (CSTR) malloc(strlen((const char *) DBUser) + 1);
  175. strcpy(m_pDBUser, DBUser);
  176. m_pDBPassword = (CSTR) malloc(strlen((const char *) DBPassword) + 1);
  177. strcpy(m_pDBPassword, DBPassword);
  178. m_nPort = Port;
  179. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  180. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  181. {
  182. LOG_ERROR(mysql_error(m_pDBLink));
  183. }
  184. else
  185. {
  186. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  187. {
  188. m_bIsConnected = true;
  189. #ifdef DEBUG
  190. char temp[256];
  191. sprintf(temp, "Connected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  192. LOG(temp);
  193. #endif
  194. }
  195. else
  196. {
  197. LOG_ERROR(mysql_error(m_pDBLink));
  198. };
  199. };
  200. return m_bIsConnected;
  201. }
  202. bool MySQL_Connector::ReConnect(void)
  203. {
  204. m_bIsConnected = false;
  205. if (m_pDBLink != NULL)
  206. {
  207. mysql_close(m_pDBLink);
  208. };
  209. m_pDBLink = mysql_init(NULL);
  210. /* CLIENT_COMPRESS | CLIENT_FOUND_ROWS | CLIENT_IGNORE_SIGPIPE | CLIENT_INTERACTIVE | CLIENT_IGNORE_SPACE */
  211. if (! mysql_real_connect(m_pDBLink, m_pDBHost, m_pDBUser, m_pDBPassword, NULL, m_nPort, NULL, CLIENT_COMPRESS))
  212. {
  213. LOG_ERROR(mysql_error(m_pDBLink));
  214. }
  215. else
  216. {
  217. if (mysql_select_db(m_pDBLink, m_pDBName) == 0)
  218. {
  219. m_bIsConnected = true;
  220. #ifdef DEBUG
  221. char temp[256];
  222. sprintf(temp, "ReConnected to %s@%s:%d", m_pDBName, m_pDBHost, m_nPort);
  223. LOG(temp);
  224. #endif
  225. }
  226. else
  227. {
  228. LOG_ERROR(mysql_error(m_pDBLink));
  229. };
  230. };
  231. return m_bIsConnected;
  232. }
  233. AAssoc * MySQL_Connector::Query(CSTR QS, bool useCache)
  234. {
  235. AAssoc * RetVal = NULL;
  236. Assoc * tElement;
  237. unsigned int num_Fields;
  238. MYSQL_RES * Result = NULL;
  239. char temp[256];
  240. if (useCache) useCache = true;
  241. #ifdef DEBUG_MYSQL_QUERIES
  242. LOG(QS);
  243. #endif
  244. m_mAccessMutex.lock();
  245. /*
  246. if( mysql_query( m_pDBLink, QS ) ){
  247. LOG_ERROR( mysql_error( m_pDBLink ));
  248. char temp[256];
  249. sprintf( temp, "Error Code: %d", mysql_errno( m_pDBLink ));
  250. LOG_ERROR( temp );
  251. switch( mysql_errno( m_pDBLink ) ){
  252. case 1156:
  253. case 2006:
  254. case 2013
  255. case 2014:{// Server Gone
  256. if( ReConnect() ){
  257. if ( !mysql_query( m_pDBLink, QS ) ){
  258. Result = mysql_store_result( m_pDBLink );
  259. } else {
  260. LOG_ERROR( mysql_error( m_pDBLink ));
  261. sprintf( temp, "Reconnect Error Code: %d", mysql_errno( m_pDBLink ));
  262. LOG_ERROR( temp );
  263. };
  264. };
  265. break;
  266. };
  267. };
  268. } else {
  269. Result = mysql_store_result( m_pDBLink );
  270. };*/
  271. int tRetries = 0;
  272. while (mysql_query(m_pDBLink, QS))
  273. {
  274. LOG_ERROR(mysql_error(m_pDBLink));
  275. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  276. LOG_ERROR(temp);
  277. switch (mysql_errno(m_pDBLink))
  278. {
  279. case 1156:
  280. case 2006:
  281. case 2013:
  282. case 2014:
  283. {
  284. // Bounded: with the server down this looped forever while holding the access mutex,
  285. // blocking every thread that uses the database
  286. if (tRetries++ < MYSQL_MAX_RECONNECT_ATTEMPTS)
  287. {
  288. ReConnect();
  289. continue;
  290. }
  291. break;
  292. };
  293. };
  294. break;
  295. };
  296. Result = mysql_store_result(m_pDBLink);
  297. if (Result != NULL)
  298. {
  299. if (mysql_num_rows(Result))
  300. {
  301. RetVal = new AAssoc();
  302. num_Fields = mysql_num_fields(Result);
  303. MYSQL_FIELD * Fields;
  304. MYSQL_ROW Row;
  305. Fields = mysql_fetch_fields(Result);
  306. while ((Row = mysql_fetch_row(Result)) != NULL)
  307. {
  308. tElement = new Assoc();
  309. string Key;
  310. string Val;
  311. for (uint32_t i = 0; i < num_Fields; i++)
  312. {
  313. Key = string(Fields[i].name);
  314. if (Row[i] != NULL)
  315. {
  316. Val = string(Row[i]);
  317. }
  318. else
  319. {
  320. Val = string("0");
  321. };
  322. (*tElement)[Key] = Val;
  323. };
  324. /*for (map<string, string>::iterator p = (*tElement).begin( ); p != (*tElement).end( ); ++p ) {
  325. cout << "Field: " << p->first<< ", #: " << p->second << endl;
  326. } */
  327. RetVal->push_back(tElement);
  328. };
  329. }
  330. else
  331. {
  332. #ifdef DEBUG_MYSQL_QUERIES
  333. LOG("No Rows");
  334. #endif
  335. RetVal = NULL;
  336. };
  337. mysql_free_result(Result);
  338. Result = NULL;
  339. }
  340. else
  341. {
  342. #ifdef DEBUG_MYSQL_QUERIES
  343. LOG("Empty Result");
  344. #endif
  345. RetVal = NULL;
  346. };
  347. m_mAccessMutex.unlock();
  348. return RetVal;
  349. }
  350. CSTR MySQL_Connector::escapeString(CSTR str)
  351. {
  352. int len = strlen(str);
  353. CSTR temp = (CSTR) malloc(len * 2 + 1);
  354. if (temp)
  355. {
  356. m_mAccessMutex.lock();
  357. mysql_real_escape_string(m_pDBLink, temp, str, len);
  358. m_mAccessMutex.unlock();
  359. };
  360. return temp;
  361. }
  362. CSTR MySQL_Connector::escapeBinary(uint8_t * Data, uint32_t Size)
  363. {
  364. CSTR temp = NULL;
  365. if (Data && Size)
  366. {
  367. temp = (CSTR) malloc(Size * 2 + 1);
  368. if (temp)
  369. {
  370. m_mAccessMutex.lock();
  371. mysql_real_escape_string(m_pDBLink, temp, (const char *) Data, Size);
  372. m_mAccessMutex.unlock();
  373. };
  374. };
  375. return temp;
  376. }
  377. /*
  378. Non-blocking query without logging, for the database log service: it runs inside the log broadcast
  379. (log mutex held) and must not wait for the access mutex - a thread that holds the access mutex and
  380. logs an error waits for the log mutex (deadlock), and on the same thread the non-recursive access
  381. mutex would deadlock by itself. Returns false when the connection is busy.
  382. */
  383. bool MySQL_Connector::TrySilentQuery(CSTR QS)
  384. {
  385. if (! m_mAccessMutex.try_lock())
  386. {
  387. return false;
  388. }
  389. bool RetVal = (mysql_query(m_pDBLink, QS) == 0);
  390. if (RetVal)
  391. {
  392. MYSQL_RES * tRes = mysql_store_result(m_pDBLink); // INSERT: normally none
  393. if (tRes)
  394. {
  395. mysql_free_result(tRes);
  396. }
  397. }
  398. m_mAccessMutex.unlock();
  399. return RetVal;
  400. }
  401. uint64_t MySQL_Connector::QueryID(CSTR QS)
  402. {
  403. uint64_t RetVal = 0;
  404. char temp[256];
  405. m_mAccessMutex.lock();
  406. #ifdef DEBUG_MYSQL_QUERIES
  407. LOG(QS);
  408. #endif
  409. int tRetries = 0;
  410. while (mysql_query(m_pDBLink, QS))
  411. {
  412. LOG_ERROR(mysql_error(m_pDBLink));
  413. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  414. LOG_ERROR(temp);
  415. switch (mysql_errno(m_pDBLink))
  416. {
  417. case 1156:
  418. case 2006:
  419. case 2013:
  420. case 2014:
  421. {
  422. // Bounded: with the server down this looped forever while holding the access mutex,
  423. // blocking every thread that uses the database
  424. if (tRetries++ < MYSQL_MAX_RECONNECT_ATTEMPTS)
  425. {
  426. ReConnect();
  427. continue;
  428. }
  429. break;
  430. };
  431. };
  432. break;
  433. };
  434. RetVal = (uint64_t) mysql_insert_id(m_pDBLink);
  435. m_mAccessMutex.unlock();
  436. return RetVal;
  437. }
  438. void MySQL_Connector::SilentQuery(CSTR QS)
  439. {
  440. char temp[256];
  441. m_mAccessMutex.lock();
  442. #ifdef DEBUG_MYSQL_QUERIES
  443. LOG(QS);
  444. #endif
  445. int tRetries = 0;
  446. while (mysql_query(m_pDBLink, QS))
  447. {
  448. LOG_ERROR(mysql_error(m_pDBLink));
  449. sprintf(temp, "Error Code: %d", mysql_errno(m_pDBLink));
  450. LOG_ERROR(temp);
  451. switch (mysql_errno(m_pDBLink))
  452. {
  453. case 1156:
  454. case 2006:
  455. case 2013:
  456. case 2014:
  457. {
  458. // Bounded: with the server down this looped forever while holding the access mutex,
  459. // blocking every thread that uses the database
  460. if (tRetries++ < MYSQL_MAX_RECONNECT_ATTEMPTS)
  461. {
  462. ReConnect();
  463. continue;
  464. }
  465. break;
  466. };
  467. };
  468. break;
  469. };
  470. m_mAccessMutex.unlock();
  471. }
  472. void deleteResult(AAssoc *& R)
  473. {
  474. if (R)
  475. {
  476. while (R->size())
  477. {
  478. delete((Assoc *) R->at(0));
  479. R->erase(R->begin());
  480. };
  481. R->clear();
  482. delete(R);
  483. R = NULL;
  484. };
  485. }
  486. #undef LOG
  487. #undef LOG_ERROR