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.

103 lines
5.0 KiB

2 years ago
​
​
​
2 years ago
​
​
​
2 years ago
2 years ago
2 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
2 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
2 hours ago
​
​
12 months ago
​
​
2 years ago
​
​
​
​
​
​
​
12 months 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
2 hours ago
2 years ago
​
​
2 years ago
​
12 months ago
2 years ago
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
12 months ago
2 years ago
​
​
​
  1. #ifndef __ARGUS_RPC_CLIENT_HANDLER__
  2. #define __ARGUS_RPC_CLIENT_HANDLER__
  3. #include "../../data/net/netClientHandler.h"
  4. #include "../../data/protocol/protocol.h"
  5. #include "../../data/protocol/protocolARGUS_DATA.h"
  6. #include "../../components/base/basicThread.h"
  7. #include "../common/serverMySQL_dataBinder.h"
  8. #include "../serva/servaSessionManager.h"
  9. #include "../../components/base/basicEvent.h"
  10. #include "../../components/base/basicEventHeap.h"
  11. class servaRemoteClient;
  12. typedef struct S_SESSION_EVENT{
  13. uint64_t db_id;
  14. }SESSION_EVENT;
  15. typedef std::vector<SESSION_EVENT> V_SESSION_EVENTS;
  16. typedef struct S_RPC_CMD_RECORD{
  17. uint8_t CMD;
  18. size_t DATA_SIZE;
  19. void * DATA;
  20. }RPC_CMD_RECORD;
  21. typedef basicRing<RPC_CMD_RECORD> RPC_CMD_QUEUE;
  22. class servaVideoTransferHelper{
  23. private:
  24. basicLogCollection * m_pLog;
  25. char m_cCurrentFile[MAX_PATH];
  26. uint64_t m_nRemoteRefID;
  27. FILE * m_pFD;
  28. ARGUS_DATA_VIDEO_INFO m_sIn;
  29. uint64_t m_nSAVED;
  30. uint8_t m_bOPerc;
  31. basicEventHeap * m_pEventHeap;
  32. //uint32_t m_nBufferSize;
  33. //uint8_t * m_pBuffer;
  34. //uint64_t m_nCTSMS; // future use for Target Binding ( history filtering )
  35. public:
  36. servaVideoTransferHelper();
  37. ~servaVideoTransferHelper();
  38. uint32_t saveNextChunk( uint8_t *ptr, size_t avail );
  39. void clear();
  40. bool init( CSTR base, uint64_t MID, uint64_t SID, ARGUS_DATA_VIDEO_INFO *ptr );
  41. uint64_t getReferenceID(){ return m_nRemoteRefID; };
  42. };
  43. // A connection that sends nothing for this long is dead (la_mojo sends a status about every second).
  44. // Half-open connections (module powered off, cable/VPN down) are never reported by the socket itself.
  45. #define RPC_IDLE_TIMEOUT_S 60
  46. #define TRAIN_ON_TRACK true
  47. #define TRAIN_IS_GONE false
  48. class servaRPCHadnler: public basicThread, public networkClientHandler, public protocolParser{
  49. private:
  50. servaRemoteClient * m_pMaster;
  51. servaMySQLDBC * m_pDBC;
  52. basicLogCollection * m_pLog;
  53. servaSessionManager * m_pSM;
  54. basicEventHeap * m_pEventHeap;
  55. char m_cRemoteIP[64];
  56. char m_cRemoteIdentity[64];
  57. bool m_bClientStructsArePacked;
  58. uint64_t m_dMojoDBID;
  59. uint64_t m_dTrackDBID;
  60. uint64_t m_dSessionDBID;
  61. uint32_t m_nInbondSerial;
  62. uint32_t m_nOutbondSerial;
  63. //bool m_bSessionIsActive;
  64. V_SESSION_EVENTS m_vSessionPool;
  65. servaVideoTransferHelper m_pVideoHelper;
  66. basicMutex m_mCloudProtector;
  67. uint32_t m_nCloudStatus;
  68. uint32_t m_nPreTrainCloudStatus;
  69. bool m_bTrainOnTrack;
  70. volatile time_t m_tLastRx; // time of the last received packet
  71. bool peer_send_ack_evt( uint64_t evt_dbid, CSTR uuid );
  72. bool peer_send_req_draw( uint8_t sensor, uint8_t draw_type = ARGUS_RPC_DRAW_TYPE_FAST, bool getexisted = true );
  73. bool peer_send_byte_status( uint8_t status );
  74. bool peer_send_reg_ack( DBC_REG_MODULE_DATA *ptr );
  75. bool peer_send_rpc( ARGUS_DATA_RPC *ptr );
  76. bool peer_send_time_sync_ack( ARGUS_DATA_TIME_SYNC_ACK *ptr );
  77. bool peer_send_vdata_ack( uint8_t status );
  78. bool peer_send_vinfo_ack( uint64_t pid );
  79. bool setTrainPresense( bool TST );
  80. protected:
  81. virtual int packetChecker(U8 *Buffer, size_t BufferSize);
  82. virtual protocolPackage * packetExtractor(U8 *Buffer, size_t BufferSize);
  83. virtual bool packetHandler( protocolPackage *Package );
  84. virtual void m_fThreadProc( void );
  85. public:
  86. explicit servaRPCHadnler( int SocketFD, networkServiceEngine *ptr );
  87. ~servaRPCHadnler();
  88. virtual bool attachEngine( networkServiceEngine *ptr );
  89. virtual bool dispatchEvent( EVENT_CONTAINER *Evt );
  90. virtual bool getStatus( void );
  91. bool setRemotePeerIP( CSTR ptr );
  92. bool getTrainStatus(){ return m_bTrainOnTrack; };
  93. };
  94. #endif