Skip to content

RUM-1330 Make sure we use try-locks in our NDK signal catcher#1665

Merged
mariusc83 merged 1 commit into
developfrom
mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic
Oct 12, 2023
Merged

RUM-1330 Make sure we use try-locks in our NDK signal catcher#1665
mariusc83 merged 1 commit into
developfrom
mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic

Conversation

@mariusc83

Copy link
Copy Markdown
Member

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 mutex lock 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 use try locks instead of simple locks.

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)

  • Feature or bugfix MUST have appropriate tests (unit, integration, e2e)
  • Make sure you discussed the feature or bugfix with the maintaining team in an Issue
  • Make sure each commit and the PR mention the Issue number (cf the CONTRIBUTING doc)

@mariusc83
mariusc83 force-pushed the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch from 70e0fc3 to 35753fd Compare October 10, 2023 15:24
@mariusc83
mariusc83 marked this pull request as ready for review October 10, 2023 15:24
@mariusc83
mariusc83 requested a review from a team as a code owner October 10, 2023 15:24
#include "utils/string-utils.h"


#ifndef NDEBUG

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually don't think we need this directive check here but just in case

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably do, in fact the whole test would probably need to be NDEBUG specific

@mariusc83
mariusc83 force-pushed the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch from 35753fd to e21c2a6 Compare October 10, 2023 15:32

static const char *LOG_TAG = "DatadogNdkCrashReporter";

#ifndef NDEBUG

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is moved before, check in the modified file

#include "utils/string-utils.h"


#ifndef NDEBUG

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably do, in fact the whole test would probably need to be NDEBUG specific

@codecov-commenter

codecov-commenter commented Oct 10, 2023

Copy link
Copy Markdown

Codecov Report

Merging #1665 (1737ba4) into develop (a5d678d) will decrease coverage by 0.01%.
The diff coverage is n/a.

@@             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     

see 16 files with indirect coverage changes

0xnm
0xnm previously approved these changes Oct 11, 2023
pthread_mutex_unlock(&handler_mutex);
return;
}
if(pthread_mutex_trylock(&handler_mutex)!=0){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(pthread_mutex_trylock(&handler_mutex)!=0){
if(pthread_mutex_trylock(&handler_mutex) != 0){

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(pthread_mutex_trylock(&handler_mutex)!=0){
if(pthread_mutex_trylock(&handler_mutex) != 0){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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){

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(pthread_mutex_trylock(&mutex)!=0){
if(pthread_mutex_trylock(&mutex) != 0){

@mariusc83
mariusc83 force-pushed the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch from e21c2a6 to 8c29b47 Compare October 11, 2023 07:47
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mariusc83

Copy link
Copy Markdown
Member Author

@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.

@xgouchet

Copy link
Copy Markdown
Contributor

@mariusc83 in that case, then maybe add a comment explaining that 👍

@mariusc83
mariusc83 force-pushed the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch 2 times, most recently from 717057a to e510444 Compare October 11, 2023 08:55
val errorMessage = forge.anAlphabeticalString()
val errorStack = forge.anAlphabeticalString()
val fakeSignal = forge.aPositiveInt(true)
val fakeSignalName = forge.anAlphabeticalString(size = 20)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Can you use a more random thing than 20, or at least use a constant explaining the 30 characters constraint

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mariusc83
mariusc83 force-pushed the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch from e510444 to 1737ba4 Compare October 11, 2023 13:28
@mariusc83
mariusc83 requested a review from xgouchet October 11, 2023 13:29
@mariusc83
mariusc83 merged commit 1fc5582 into develop Oct 12, 2023
@mariusc83
mariusc83 deleted the mconstantin/rum-1330/use-try-catch-lock-in-ndk-signal-catcher-logic branch October 12, 2023 07:14
@xgouchet xgouchet added this to the 2.3.0 milestone Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants