RUM-1330 Make sure we use try-locks in our NDK signal catcher#1665
Conversation
70e0fc3 to
35753fd
Compare
| #include "utils/string-utils.h" | ||
|
|
||
|
|
||
| #ifndef NDEBUG |
There was a problem hiding this comment.
I actually don't think we need this directive check here but just in case
There was a problem hiding this comment.
We probably do, in fact the whole test would probably need to be NDEBUG specific
35753fd to
e21c2a6
Compare
|
|
||
| static const char *LOG_TAG = "DatadogNdkCrashReporter"; | ||
|
|
||
| #ifndef NDEBUG |
There was a problem hiding this comment.
I feel like this is a lot of changes just to support the test. Maybe it makes sense to leave the mutex as static and instead create an NDEBUG wrapped function just to lock it.
|
|
||
| // sync everything | ||
| pthread_mutex_lock(&handler_mutex); | ||
| if (tracking_consent != tracking_consent_granted) { |
There was a problem hiding this comment.
Is there any reason we can't move this check up before the mutex lock? I wouldn't think we'd care about a race condition here.
There was a problem hiding this comment.
it is moved before, check in the modified file
| #include "utils/string-utils.h" | ||
|
|
||
|
|
||
| #ifndef NDEBUG |
There was a problem hiding this comment.
We probably do, in fact the whole test would probably need to be NDEBUG specific
Codecov Report
@@ Coverage Diff @@
## develop #1665 +/- ##
===========================================
- Coverage 83.73% 83.72% -0.01%
===========================================
Files 459 459
Lines 15778 15778
Branches 2354 2354
===========================================
- Hits 13211 13209 -2
- Misses 1938 1939 +1
- Partials 629 630 +1 |
| pthread_mutex_unlock(&handler_mutex); | ||
| return; | ||
| } | ||
| if(pthread_mutex_trylock(&handler_mutex)!=0){ |
There was a problem hiding this comment.
| if(pthread_mutex_trylock(&handler_mutex)!=0){ | |
| if(pthread_mutex_trylock(&handler_mutex) != 0){ |
There was a problem hiding this comment.
I usually apply ktlintFormat at the end of my PR but of course for C code doesn't work :)
| jstring storage_path) { | ||
| using namespace stringutils; | ||
| pthread_mutex_lock(&handler_mutex); | ||
| if(pthread_mutex_trylock(&handler_mutex)!=0){ |
There was a problem hiding this comment.
| if(pthread_mutex_trylock(&handler_mutex)!=0){ | |
| if(pthread_mutex_trylock(&handler_mutex) != 0){ |
There was a problem hiding this comment.
Warning
According to the documentation for pthread_mutex_trylock, there might be several return value. Maybe it'd be relevant to actually look at the return value and act accordingly.
If successful, the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error.
| // It may happen that the process is killed during this function execution. | ||
| // Therefore this function may never return. | ||
| pthread_mutex_lock(&mutex); | ||
| if(pthread_mutex_trylock(&mutex)!=0){ |
There was a problem hiding this comment.
| if(pthread_mutex_trylock(&mutex)!=0){ | |
| if(pthread_mutex_trylock(&mutex) != 0){ |
e21c2a6 to
8c29b47
Compare
| fun mustWriteAnErrorLog_whenHandlingSignal_whenConsentUpdatedToGranted() { | ||
| val fakeSignal = forge.anInt(min = 1, max = 32) | ||
| // we need to keep this this size because we are using a buffer of [30] size in c++ for | ||
| // we need to keep this size because we are using a buffer of [30] size in c++ for |
There was a problem hiding this comment.
Note
You're talking about the (size = 20) below right? Wouldn't it be better to have a constant const val SIGNAL_SIZE = 20 (declared in the companion object for example) and reuse it everywhere?
Or because we know the size can be at most 30, user something like val signalSize = forge.anInt(1, 30)?
There was a problem hiding this comment.
I actually need to check this but I think this is not needed anymore and I need to just forge a positive int there as the serialization was changed
| jstring storage_path) { | ||
| using namespace stringutils; | ||
| pthread_mutex_lock(&handler_mutex); | ||
| if(pthread_mutex_trylock(&handler_mutex)!=0){ |
There was a problem hiding this comment.
Warning
According to the documentation for pthread_mutex_trylock, there might be several return value. Maybe it'd be relevant to actually look at the return value and act accordingly.
If successful, the pthread_mutex_lock() and pthread_mutex_unlock() functions shall return zero; otherwise, an error number shall be returned to indicate the error.
|
@xgouchet cannot reply on those comments for some reason so I will reply here, I've read the docs before adding this logic and actually we do not want to do anything in case we cannot acquire the lock, we just do not write the error log as there is no other action that we can take in that case. We could not find out who's acquiring that lock in the tests we performed (me and @fuzzybinary) and we do not want to block the process in the signal catcher in this case. |
|
@mariusc83 in that case, then maybe add a comment explaining that 👍 |
717057a to
e510444
Compare
| val errorMessage = forge.anAlphabeticalString() | ||
| val errorStack = forge.anAlphabeticalString() | ||
| val fakeSignal = forge.aPositiveInt(true) | ||
| val fakeSignalName = forge.anAlphabeticalString(size = 20) |
There was a problem hiding this comment.
Note
Can you use a more random thing than 20, or at least use a constant explaining the 30 characters constraint
There was a problem hiding this comment.
We actually do not need these limitations anymore as this was needed before when that part of code was written in pure C and we used fixed length memory allocation for that. Currently that code is C++ and allows dynamic allocation.
e510444 to
1737ba4
Compare
What does this PR do?
We recently had some reported issues in the flutter sdk regarding some ANRs caused by our signal catcher. Basically what was happening is that our code inside the signal catcher was trying to acquire a
mutexlock which was already acquired by another thread (we could not really reproduce this). We chose to go with the safest mechanism here in order to avoid any other issues and usetry locksinstead of simplelocks.DataDog/dd-sdk-flutter#487
Motivation
What inspired you to submit this pull request?
Additional Notes
Anything else we should know when reviewing?
Review checklist (to be filled by reviewers)