@@ -418,7 +418,6 @@ static void freeDataToSendQueue( DataToSendQueue* dataQueue )
418
418
419
419
struct BackgroundBackendComm
420
420
{
421
- int refCount ;
422
421
Mutex * mutex ;
423
422
ConditionVariable * condVar ;
424
423
Thread * thread ;
@@ -730,8 +729,10 @@ void* backgroundBackendCommThreadFunc( void* arg )
730
729
goto finally ;
731
730
}
732
731
733
- ResultCode unwindBackgroundBackendComm ( BackgroundBackendComm * * backgroundBackendCommOutPtr , const TimeSpec * timeoutAbsUtc )
732
+ ResultCode unwindBackgroundBackendComm ( BackgroundBackendComm * * backgroundBackendCommOutPtr , const TimeSpec * timeoutAbsUtc , bool isCreatedByThisProcess )
734
733
{
734
+ ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG ( "isCreatedByThisProcess: %s" , boolToString ( isCreatedByThisProcess ) );
735
+
735
736
ELASTIC_APM_ASSERT_VALID_PTR ( backgroundBackendCommOutPtr );
736
737
// ELASTIC_APM_ASSERT_VALID_PTR( timeoutAbsUtc ); <- timeoutAbsUtc can be NULL
737
738
@@ -744,12 +745,20 @@ ResultCode unwindBackgroundBackendComm( BackgroundBackendComm** backgroundBacken
744
745
goto finally ;
745
746
}
746
747
748
+ if ( ! isCreatedByThisProcess )
749
+ {
750
+ ELASTIC_APM_LOG_DEBUG ( "Deallocating memory related to background communication data structures inherited from parent process after fork"
751
+ " without actually properly destroying synchronization primitives since it's impossible to do in child process"
752
+ "; parent PID: %d"
753
+ , (int )getParentProcessId () );
754
+ }
755
+
747
756
if ( backgroundBackendComm -> thread != NULL )
748
757
{
749
758
void * backgroundBackendCommThreadFuncRetVal = NULL ;
750
759
bool hasTimedOut ;
751
760
ELASTIC_APM_CALL_IF_FAILED_GOTO (
752
- timedJoinAndDeleteThread ( & ( backgroundBackendComm -> thread ), & backgroundBackendCommThreadFuncRetVal , timeoutAbsUtc , & hasTimedOut , __FUNCTION__ ) );
761
+ timedJoinAndDeleteThread ( & ( backgroundBackendComm -> thread ), & backgroundBackendCommThreadFuncRetVal , timeoutAbsUtc , isCreatedByThisProcess , & hasTimedOut , __FUNCTION__ ) );
753
762
if ( hasTimedOut )
754
763
{
755
764
ELASTIC_APM_LOG_ERROR ( "Join to thread for background backend communications timed out - skipping the rest of cleanup and exiting" );
@@ -759,7 +768,7 @@ ResultCode unwindBackgroundBackendComm( BackgroundBackendComm** backgroundBacken
759
768
760
769
if ( backgroundBackendComm -> condVar != NULL )
761
770
{
762
- ELASTIC_APM_CALL_IF_FAILED_GOTO ( deleteConditionVariable ( & ( backgroundBackendComm -> condVar ) ) );
771
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( deleteConditionVariable ( & ( backgroundBackendComm -> condVar ), isCreatedByThisProcess ) );
763
772
}
764
773
765
774
if ( backgroundBackendComm -> mutex != NULL )
@@ -772,13 +781,13 @@ ResultCode unwindBackgroundBackendComm( BackgroundBackendComm** backgroundBacken
772
781
ELASTIC_APM_FREE_INSTANCE_AND_SET_TO_NULL ( BackgroundBackendComm , * backgroundBackendCommOutPtr );
773
782
774
783
finally :
784
+ ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT ();
775
785
return resultCode ;
776
786
777
787
failure :
778
788
goto finally ;
779
789
}
780
790
781
- static Mutex * g_backgroundBackendCommMutex = NULL ;
782
791
static BackgroundBackendComm * g_backgroundBackendComm = NULL ;
783
792
784
793
static bool deriveAsyncBackendComm ( const ConfigSnapshot * config , String * dbgReason )
@@ -799,47 +808,6 @@ static bool deriveAsyncBackendComm( const ConfigSnapshot* config, String* dbgRea
799
808
return true;
800
809
}
801
810
802
- ResultCode backgroundBackendCommOnModuleInit ( const ConfigSnapshot * config )
803
- {
804
- ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG ( "g_backgroundBackendCommMutex: %p; g_backgroundBackendComm: %p"
805
- , g_backgroundBackendCommMutex , g_backgroundBackendComm );
806
-
807
- ResultCode resultCode ;
808
-
809
- if ( g_backgroundBackendCommMutex != NULL )
810
- {
811
- ELASTIC_APM_LOG_ERROR ( "Unexpected state: g_backgroundBackendCommMutex != NULL" );
812
- ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE ();
813
- }
814
-
815
- if ( g_backgroundBackendComm != NULL )
816
- {
817
- ELASTIC_APM_LOG_ERROR ( "Unexpected state: g_backgroundBackendComm != NULL" );
818
- ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE ();
819
- }
820
-
821
- String dbgAsyncBackendCommReason = NULL ;
822
- if ( ! deriveAsyncBackendComm ( config , & dbgAsyncBackendCommReason ) )
823
- {
824
- ELASTIC_APM_LOG_DEBUG ( "async_backend_comm (asyncBackendComm) configuration option is %s - no need to start background backend comm" , dbgAsyncBackendCommReason );
825
- resultCode = resultSuccess ;
826
- goto finally ;
827
- }
828
-
829
- ELASTIC_APM_CALL_IF_FAILED_GOTO ( newMutex ( & ( g_backgroundBackendCommMutex ), /* dbgDesc */ "g_backgroundBackendCommMutex" ) );
830
-
831
- finally :
832
- ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT ();
833
- return resultCode ;
834
-
835
- failure :
836
- if ( g_backgroundBackendCommMutex != NULL )
837
- {
838
- deleteMutex ( & g_backgroundBackendCommMutex );
839
- }
840
- goto finally ;
841
- }
842
-
843
811
ResultCode newBackgroundBackendComm ( const ConfigSnapshot * config , BackgroundBackendComm * * backgroundBackendCommOut )
844
812
{
845
813
ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY ();
@@ -848,7 +816,6 @@ ResultCode newBackgroundBackendComm( const ConfigSnapshot* config, BackgroundBac
848
816
BackgroundBackendComm * backgroundBackendComm = NULL ;
849
817
850
818
ELASTIC_APM_MALLOC_INSTANCE_IF_FAILED_GOTO ( BackgroundBackendComm , /* out */ backgroundBackendComm );
851
- backgroundBackendComm -> refCount = 1 ;
852
819
backgroundBackendComm -> condVar = NULL ;
853
820
backgroundBackendComm -> mutex = NULL ;
854
821
backgroundBackendComm -> thread = NULL ;
@@ -860,7 +827,6 @@ ResultCode newBackgroundBackendComm( const ConfigSnapshot* config, BackgroundBac
860
827
ELASTIC_APM_CALL_IF_FAILED_GOTO ( newMutex ( & ( backgroundBackendComm -> mutex ), /* dbgDesc */ "Background backend communications" ) );
861
828
ELASTIC_APM_CALL_IF_FAILED_GOTO ( newConditionVariable ( & ( backgroundBackendComm -> condVar ), /* dbgDesc */ "Background backend communications" ) );
862
829
863
- backgroundBackendComm -> refCount = 2 ;
864
830
resultCode = newThread ( & ( backgroundBackendComm -> thread )
865
831
, & backgroundBackendCommThreadFunc
866
832
, /* threadFuncArg: */ backgroundBackendComm
@@ -869,10 +835,6 @@ ResultCode newBackgroundBackendComm( const ConfigSnapshot* config, BackgroundBac
869
835
{
870
836
ELASTIC_APM_LOG_DEBUG ( "Started thread for background backend communications; thread ID: %" PRIu64 , getThreadId ( backgroundBackendComm -> thread ) );
871
837
}
872
- else
873
- {
874
- -- backgroundBackendComm -> refCount ;
875
- }
876
838
877
839
resultCode = resultSuccess ;
878
840
* backgroundBackendCommOut = backgroundBackendComm ;
@@ -882,31 +844,15 @@ ResultCode newBackgroundBackendComm( const ConfigSnapshot* config, BackgroundBac
882
844
return resultCode ;
883
845
884
846
failure :
885
- unwindBackgroundBackendComm ( & backgroundBackendComm , /* timeoutAbsUtc: */ NULL );
847
+ unwindBackgroundBackendComm ( & backgroundBackendComm , /* timeoutAbsUtc: */ NULL , /* isCreatedByThisProcess */ true );
886
848
goto finally ;
887
849
}
888
850
889
- ResultCode backgroundBackendCommOnRequestInit ( const ConfigSnapshot * config )
851
+ ResultCode backgroundBackendCommEnsureInited ( const ConfigSnapshot * config )
890
852
{
891
- ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG ( "g_backgroundBackendCommMutex: %p" , g_backgroundBackendCommMutex );
853
+ ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY ( );
892
854
893
855
ResultCode resultCode ;
894
- bool shouldUnlockMutex = false;
895
-
896
- String dbgAsyncBackendCommReason = NULL ;
897
- if ( ! deriveAsyncBackendComm ( config , & dbgAsyncBackendCommReason ) )
898
- {
899
- ELASTIC_APM_LOG_DEBUG ( "async_backend_comm (asyncBackendComm) configuration option is %s - no need to start background backend comm" , dbgAsyncBackendCommReason );
900
- resultCode = resultSuccess ;
901
- goto finally ;
902
- }
903
-
904
- if ( g_backgroundBackendCommMutex == NULL )
905
- {
906
- ELASTIC_APM_LOG_ERROR ( "Unexpected state: g_backgroundBackendCommMutex == NULL" );
907
- ELASTIC_APM_SET_RESULT_CODE_AND_GOTO_FAILURE ();
908
- }
909
- ELASTIC_APM_CALL_IF_FAILED_GOTO ( lockMutex ( g_backgroundBackendCommMutex , & shouldUnlockMutex , __FUNCTION__ ) );
910
856
911
857
if ( g_backgroundBackendComm == NULL )
912
858
{
@@ -916,7 +862,6 @@ ResultCode backgroundBackendCommOnRequestInit( const ConfigSnapshot* config )
916
862
resultCode = resultSuccess ;
917
863
918
864
finally :
919
- unlockMutex ( g_backgroundBackendCommMutex , & shouldUnlockMutex , __FUNCTION__ );
920
865
ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT ();
921
866
return resultCode ;
922
867
@@ -965,19 +910,16 @@ void backgroundBackendCommOnModuleShutdown()
965
910
return ;
966
911
}
967
912
968
- ELASTIC_APM_ASSERT ( g_backgroundBackendCommMutex != NULL , "%p" , g_backgroundBackendCommMutex );
969
-
970
913
ResultCode resultCode ;
971
914
TimeSpec shouldExitBy ;
972
915
973
916
ELASTIC_APM_CALL_IF_FAILED_GOTO ( signalBackgroundBackendCommThreadToExit ( backgroundBackendComm , /* out */ & shouldExitBy ) );
974
- ELASTIC_APM_CALL_IF_FAILED_GOTO ( unwindBackgroundBackendComm ( & backgroundBackendComm , & shouldExitBy ) );
917
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( unwindBackgroundBackendComm ( & backgroundBackendComm , & shouldExitBy , /* isCreatedByThisProcess */ true ) );
975
918
resultCode = resultSuccess ;
976
919
977
920
finally :
978
921
979
922
g_backgroundBackendComm = NULL ;
980
- ELASTIC_APM_CALL_IF_FAILED_GOTO ( deleteMutex ( & g_backgroundBackendCommMutex ) );
981
923
return ;
982
924
983
925
failure :
@@ -1082,11 +1024,8 @@ ResultCode sendEventsToApmServerWithDataConvertedForSync(
1082
1024
, serializedEvents ) );
1083
1025
1084
1026
resultCode = resultSuccess ;
1085
-
1086
1027
finally :
1087
-
1088
1028
freeMallocedStringBuffer ( /* in,out */ & userAgentHttpHeaderWithTermNull );
1089
-
1090
1029
return resultCode ;
1091
1030
1092
1031
failure :
@@ -1100,7 +1039,9 @@ ResultCode sendEventsToApmServer(
1100
1039
, StringView userAgentHttpHeader
1101
1040
, StringView serializedEvents )
1102
1041
{
1042
+ ResultCode resultCode ;
1103
1043
long serverTimeoutMillisecondsLong = (long ) ceil ( serverTimeoutMilliseconds );
1044
+
1104
1045
ELASTIC_APM_LOG_DEBUG (
1105
1046
"Handling request to send events..."
1106
1047
" disableSend: %s"
@@ -1113,15 +1054,49 @@ ResultCode sendEventsToApmServer(
1113
1054
, (UInt64 ) serializedEvents .length , (int ) serializedEvents .length , serializedEvents .begin );
1114
1055
1115
1056
String dbgAsyncBackendCommReason = NULL ;
1116
- if ( ! deriveAsyncBackendComm ( config , & dbgAsyncBackendCommReason ) )
1057
+ bool shouldSendAsync = deriveAsyncBackendComm ( config , & dbgAsyncBackendCommReason );
1058
+ ELASTIC_APM_LOG_DEBUG ( "async_backend_comm (asyncBackendComm) configuration option is %s - sending events %s"
1059
+ , dbgAsyncBackendCommReason , ( shouldSendAsync ? "asynchronously" : "synchronously" ) );
1060
+ if ( shouldSendAsync )
1061
+ {
1062
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( backgroundBackendCommEnsureInited ( config ) );
1063
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( enqueueEventsToSendToApmServer ( disableSend , serverTimeoutMilliseconds , userAgentHttpHeader , serializedEvents ) );
1064
+ }
1065
+ else
1066
+ {
1067
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( sendEventsToApmServerWithDataConvertedForSync (
1068
+ disableSend
1069
+ , serverTimeoutMilliseconds
1070
+ , config
1071
+ , userAgentHttpHeader
1072
+ , serializedEvents ) );
1073
+ }
1074
+
1075
+ resultCode = resultSuccess ;
1076
+ finally :
1077
+ ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT ();
1078
+ return resultCode ;
1079
+
1080
+ failure :
1081
+ goto finally ;
1082
+ }
1083
+
1084
+ ResultCode resetBackgroundBackendCommStateInForkedChild ()
1085
+ {
1086
+ ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY_MSG ( "g_backgroundBackendComm %s NULL" , (g_backgroundBackendComm == NULL ) ? "==" : "!=" );
1087
+
1088
+ ResultCode resultCode ;
1089
+
1090
+ if ( g_backgroundBackendComm != NULL )
1117
1091
{
1118
- ELASTIC_APM_LOG_DEBUG ( "async_backend_comm (asyncBackendComm) configuration option is %s - sending events synchronously" , dbgAsyncBackendCommReason );
1119
- return sendEventsToApmServerWithDataConvertedForSync ( disableSend
1120
- , serverTimeoutMilliseconds
1121
- , config
1122
- , userAgentHttpHeader
1123
- , serializedEvents );
1092
+ ELASTIC_APM_CALL_IF_FAILED_GOTO ( unwindBackgroundBackendComm ( & g_backgroundBackendComm , /* timeoutAbsUtc: */ NULL , /* isCreatedByThisProcess */ false ) );
1124
1093
}
1125
1094
1126
- return enqueueEventsToSendToApmServer ( disableSend , serverTimeoutMilliseconds , userAgentHttpHeader , serializedEvents );
1127
- }
1095
+ resultCode = resultSuccess ;
1096
+ finally :
1097
+ ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT ();
1098
+ return resultCode ;
1099
+
1100
+ failure :
1101
+ goto finally ;
1102
+ }
0 commit comments