Article 5KR50 Segmentation fault while using pthread_setname_np

Segmentation fault while using pthread_setname_np

by
vijaykoundinya
from LinuxQuestions.org on (#5KR50)
I'm trying to patch thread names using the thread name libpthread.pthread_setname_np function in python like so

Code:
libpthread = ctypes.CDLL("libpthread.so")
if not libpthread:
# no pthread library, not patching
return

if not hasattr(libpthread, "pthread_setname_np"):
# pthread library does not have pthread_setname_np function, not patching
return

pthread_setname_np = libpthread.pthread_setname_np
pthread_setname_np.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
pthread_setname_np.restype = ctypes.c_int
orig_start = threading.Thread.start

def start(self):
"""Patched version of threading.Thread.start"""
orig_start(self)
try:
name = self.name
if name:
if hasattr(name, "encode"):
name = name.encode('ascii', 'replace')
ident = getattr(self, "ident", None)
if ident is not None:
pthread_setname_np(ident, name[:15])
except Exception: # pylint: disable=broad-except
pass # Don't care about failure to set nameThe following is code is proving me with a segmentation fault with the following message.

segfault at 7f95a9f1e9d0 ip 00007f95d8ab0a6b sp 00007f95b2fc00b0 error 4 in libpthread-2.31.so[7f95d8aa2000+10000]

Any inputs on what could be causing this issue.latest?d=yIl2AUoC8zA latest?i=getKb3OOPy8:jbF6TzXy_FE:F7zBnMy latest?i=getKb3OOPy8:jbF6TzXy_FE:V_sGLiP latest?d=qj6IDK7rITs latest?i=getKb3OOPy8:jbF6TzXy_FE:gIN9vFwgetKb3OOPy8
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments