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.

216 lines
8.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks 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
17 hours ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
4 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
4 weeks ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "servaLifeMonitoring.h"
  2. #include "../../components/base/basicEvent.h"
  3. #include "../../components/base/basicEventHeap.h"
  4. #define LOG( X ) m_pLog->Log( "HELZ", this, X )
  5. #ifdef DEBUG
  6. #define LOG_ERROR( X ) {\
  7. char _tBfrErrMSG2w34[256];\
  8. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  9. m_pLog->Log( "HELZ", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  10. }
  11. #else
  12. #define LOG_ERROR( X ) m_pLog->Log( "HELZ", this, _LOG_ERROR, X );
  13. #endif
  14. servaLifeMonitoring *servaLifeMonitoring::_Instance = 0;
  15. servaLifeMonitoring *servaLifeMonitoring::getInstance(){
  16. if( _Instance == 0 ){
  17. _Instance = new servaLifeMonitoring();
  18. };
  19. return _Instance;
  20. };
  21. servaLifeMonitoring::servaLifeMonitoring():basicValidatable(),basicThread(){
  22. m_pLog = basicLogCollection::getInstance();
  23. m_pDBC = servaMySQLDBC::getInstance();
  24. DBC_UINT64_LIST *tList = m_pDBC->listPlatforms('E');
  25. if ( tList ) {
  26. if ( tList->size() ) {
  27. for ( DBC_UINT64_LIST::iterator idx = tList->begin(); idx != tList->end(); idx++ ) {
  28. DBC_UINT64_LIST *tML = m_pDBC->listPlatformModules( *idx );
  29. if ( tML ) {
  30. if ( tML->size() ) {
  31. HEALTH_MONITORING_ZONE *tHZ = (HEALTH_MONITORING_ZONE*) malloc( sizeof( HEALTH_MONITORING_ZONE));
  32. if (tHZ) {
  33. memset( tHZ, 0, sizeof(HEALTH_MONITORING_ZONE));
  34. tHZ->platform_id = *idx;
  35. uint8_t cid = 0;
  36. tHZ->lvl = 255; // To clean prev State
  37. tHZ->validated = false;
  38. // module_id[] has 4 entries: a platform with more modules overflowed the zone
  39. for ( DBC_UINT64_LIST::iterator jdx = tML->begin(); jdx != tML->end() && cid < 4; jdx++ ) {
  40. tHZ->module_id[cid] = *jdx;
  41. tHZ->module_monitored[cid] = true;
  42. cid++;
  43. };
  44. m_mMonitoringZonesProtector.lock();
  45. m_vMonitoringZones.push_back( tHZ );
  46. m_mMonitoringZonesProtector.unlock();
  47. };
  48. };
  49. delete(tML);
  50. };
  51. };
  52. };
  53. delete( tList );
  54. };
  55. m_nStartupTS = time(NULL);
  56. init();
  57. };
  58. servaLifeMonitoring::~servaLifeMonitoring(){
  59. waitThreadEnd( true );
  60. m_mMonitoringZonesProtector.lock();
  61. if ( m_vMonitoringZones.size() ) {
  62. for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) {
  63. free( *idx );
  64. };
  65. m_vMonitoringZones.clear();
  66. };
  67. m_mMonitoringZonesProtector.unlock();
  68. };
  69. bool servaLifeMonitoring::validateZone( HEALTH_MONITORING_ZONE *ptr ){
  70. bool retVal = true;
  71. if ( ptr ) {
  72. uint8_t min_lvl = 0;
  73. uint64_t min_id = ptr->module_id[0];
  74. for ( uint8_t i = 0; i < 4; i++ ) {
  75. if (ptr->module_monitored[i]) {
  76. if( ptr->module_status[i] > min_lvl ) {
  77. min_lvl = ptr->module_status[i];
  78. min_id = ptr->module_id[i];
  79. };
  80. };
  81. };
  82. if( min_lvl != ptr->lvl ) {
  83. retVal = false;
  84. ptr->lvl = min_lvl;
  85. ptr->lvl_id = min_id;
  86. };
  87. ptr->validated = false;
  88. };
  89. return retVal;
  90. };
  91. uint8_t servaLifeMonitoring::validateModule( uint64_t module_id ){
  92. uint8_t retVal = 0;
  93. if ( module_id ) {
  94. uint32_t cts = time( NULL );
  95. uint32_t mts = m_pDBC->getCloudLTS(ARGUS_OBJECT_MODULE, module_id );
  96. uint8_t ms = m_pDBC->getCloudStatus( ARGUS_OBJECT_MODULE, module_id );
  97. if ( mts ) {
  98. if ((cts - mts) > 30) {
  99. retVal = ARGUS_LIFE_STATUS_LVL_4;
  100. } else {
  101. //Online - Check Status
  102. if( ms < ARGUS_CLOUD_STATUS_E_GENERIC ) {
  103. retVal = ARGUS_LIFE_STATUS_CLEAN;
  104. } else {
  105. switch ( ms ) {
  106. case ARGUS_CLOUD_STATUS_E_GENERIC: break;
  107. case ARGUS_CLOUD_STATUS_E_CAM_GENERIC:
  108. case ARGUS_CLOUD_STATUS_E_CAM_NC:
  109. case ARGUS_CLOUD_STATUS_E_CAM_NPTZ:
  110. case ARGUS_CLOUD_STATUS_E_CAM_NIM: retVal = ARGUS_LIFE_STATUS_LVL_1; break;
  111. case ARGUS_CLOUD_STATUS_E_SEN_GENERIC:
  112. case ARGUS_CLOUD_STATUS_E_US_LRF:
  113. case ARGUS_CLOUD_STATUS_E_BS_LRF: retVal = ARGUS_LIFE_STATUS_LVL_2; break;
  114. case ARGUS_CLOUD_STATUS_E_US_NC:
  115. case ARGUS_CLOUD_STATUS_E_BS_NC: retVal = ARGUS_LIFE_STATUS_LVL_3; break;
  116. default: retVal = ARGUS_LIFE_STATUS_LVL_3;
  117. };
  118. };
  119. };
  120. } else {
  121. retVal = ARGUS_LIFE_STATUS_LVL_4;
  122. };
  123. if ( retVal == ARGUS_LIFE_STATUS_LVL_4 ) {
  124. DBC_IS_MODULE_DATA *tData = m_pDBC->getModuleData( module_id );
  125. if ( tData ) {
  126. if( !pingIP4( tData->ip4 ) ) {
  127. #ifdef DEBUG_EXTRA
  128. LOG_ERROR("No Network Access to Platform!");
  129. #endif
  130. retVal = ARGUS_LIFE_STATUS_LVL_5;
  131. };
  132. free( tData );
  133. };
  134. };
  135. };
  136. return retVal;
  137. };
  138. bool servaLifeMonitoring::validate(){
  139. uint32_t cts = time(NULL);
  140. if ( ( cts - m_nStartupTS ) < LM_SKEEP_MONITORING_SECONDS ) return true;
  141. m_mMonitoringZonesProtector.lock();
  142. for ( V_HEALTH_MONITORING_ZONES::iterator idx = m_vMonitoringZones.begin(); idx != m_vMonitoringZones.end(); idx++ ) {
  143. if (!validateZone(*idx)) {
  144. #ifdef DEBUG
  145. LOG("Zone Health is Changed");
  146. #endif
  147. MODULE_HEALTH_STATUS *tData = (MODULE_HEALTH_STATUS*) malloc(sizeof(MODULE_HEALTH_STATUS));
  148. if ( tData ) {
  149. tData->status = (*idx)->lvl;
  150. tData->module_id = (*idx)->lvl_id;
  151. tData->platform_id = (*idx)->platform_id;
  152. #ifdef DEBUG
  153. char tT[128];
  154. sprintf( tT, "gen Event HEALTH %d", tData->status );
  155. LOG( tT );
  156. #endif
  157. EVENT_CONTAINER *tEvt = createBasicEvent(0, EVT_CLASS_SYSTEM, EVT_SRV_MODULE_HEALTH, NULL, NULL, tData );
  158. if ( tEvt ) {
  159. basicEventHeap::getInstance()->broadcast( tEvt );
  160. } else free( tData );
  161. } else LOG_ERROR("No Data Fetched from DB");
  162. #ifdef DEBUG_EXTRA
  163. } else {
  164. char tT[128];
  165. sprintf( tT, "Zone Health is same %d",(*idx)->lvl );
  166. LOG( tT );
  167. #endif
  168. };
  169. };
  170. m_mMonitoringZonesProtector.unlock();
  171. return true;
  172. };
  173. void servaLifeMonitoring::m_fThreadProc( void ){
  174. m_bThreadStatus = TH_WORKING;
  175. uint32_t ots = time(NULL);
  176. pthread_setname_np( pthread_self(), "LifeMonitoring" );
  177. while( m_bThreadStatus < TH_ENDED ) {
  178. uint32_t cts = time(NULL);
  179. if ( ( cts - m_nStartupTS ) > LM_SKEEP_MONITORING_SECONDS ){
  180. if ( ots != cts ) {
  181. ots = cts;
  182. // Taking to validate one record then return it to a queue
  183. HEALTH_MONITORING_ZONE * tCWZ = NULL;
  184. m_mMonitoringZonesProtector.lock();
  185. if ( m_vMonitoringZones.size() ) {
  186. tCWZ = m_vMonitoringZones.at(0);
  187. m_vMonitoringZones.erase(m_vMonitoringZones.begin());
  188. };
  189. m_mMonitoringZonesProtector.unlock();
  190. if ( tCWZ ) {
  191. if ( !tCWZ->validated ) {
  192. for (uint8_t i = 0; i < 4; i++) {
  193. if (tCWZ->module_monitored[i]) tCWZ->module_status[i] = validateModule( tCWZ->module_id[i] );
  194. };
  195. };
  196. m_mMonitoringZonesProtector.lock();
  197. m_vMonitoringZones.push_back( tCWZ );
  198. m_mMonitoringZonesProtector.unlock();
  199. };
  200. };
  201. };
  202. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  203. };
  204. };
  205. #undef LOG
  206. #undef LOG_ERROR