Change in osmo-trx[master]: Logger: Disable pthread cancel point inside Logger destructor

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Mon Sep 3 15:07:25 UTC 2018


Pau Espin Pedrol has uploaded this change for review. ( https://gerrit.osmocom.org/10739


Change subject: Logger: Disable pthread cancel point inside Logger destructor
......................................................................

Logger: Disable pthread cancel point inside Logger destructor

pthread_cancel is implemented in c++ using exception handlers. In
destructor of Log object, the log function is called which will
eventually call fputs() to write to a file. Since that function is
considered a cancelation point, if pthread_cancel has been called the
exception handler will start unstacking frames and calling destructors
in the process. At some point this will cause a runtime exception in c++
which will call std::terminate() to abort the process.

The solution is thus to avoid starting the cancellation process inside the
destructor.

This behavior was spotted while calling the destructor of Transceiver
object in forthcoming patches.

See a more detailed example here:
https://skaark.wordpress.com/2010/08/26/pthread_cancel-considered-harmful/

Change-Id: I71ca90f3fbc73df58b878a03361f7b7831d838b4
---
M CommonLibs/Logger.cpp
1 file changed, 3 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/39/10739/1

diff --git a/CommonLibs/Logger.cpp b/CommonLibs/Logger.cpp
index 393d882..171c635 100644
--- a/CommonLibs/Logger.cpp
+++ b/CommonLibs/Logger.cpp
@@ -44,6 +44,8 @@
 
 Log::~Log()
 {
+	int old_state;
+	pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old_state);
 	int mlen = mStream.str().size();
 	int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n');
 	const char *fmt = neednl ? "%s\n" : "%s";
@@ -51,6 +53,7 @@
 	// The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers,
 	// so just use std::cout.
 	LOGPSRC(mCategory, mPriority, filename, line, fmt, mStream.str().c_str());
+	pthread_setcancelstate(old_state, NULL);
 }
 
 ostringstream& Log::get()

-- 
To view, visit https://gerrit.osmocom.org/10739
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I71ca90f3fbc73df58b878a03361f7b7831d838b4
Gerrit-Change-Number: 10739
Gerrit-PatchSet: 1
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180903/42193cb3/attachment.htm>


More information about the gerrit-log mailing list