Skip to content

recussiveness of _thread.RLock doesn't work if lock acquired inside signal handler inside lock.acquire #144706

@xBZZZZ

Description

@xBZZZZ

Bug report

Bug description:

rlocktesting.py:

from _thread import start_new_thread,RLock
from select import select
from os import write
from signal import signal,SIGALRM,alarm

lock=RLock()

def threadfunc():
	write(1,b"thread: trying to acquire lock\n")
	lock.acquire()
	write(1,b"thread: acquired lock and doing unrelated stuff for 3 seconds\n")
	select((),(),(),3)
	write(1,b"thread: trying to release lock\n")
	lock.release()
	write(1,b"thread: released lock\n")

def SIGALRMhandler(signum,frame):
	write(1,b"SIGALRM: trying to acquire lock\n")
	lock.acquire()
	write(1,b"SIGALRM: acquired lock, returning\n")

signal(SIGALRM,SIGALRMhandler)
start_new_thread(threadfunc,())
alarm(2)
write(1,b"main: doing unrelated stuff for 1 second\n")
select((),(),(),1)
write(1,b"main: trying to acquire lock\n")
lock.acquire()#global scope
write(1,b"main: acquired lock\nEND OF USELESS PROGRAM\n")

running:

$ python3 rlocktesting.py
main: doing unrelated stuff for 1 second
thread: trying to acquire lock
thread: acquired lock and doing unrelated stuff for 3 seconds
main: trying to acquire lock
SIGALRM: trying to acquire lock
thread: trying to release lock
thread: released lock
SIGALRM: acquired lock, returning

it gets stuck forever, pressing ctrl+c:

^CTraceback (most recent call last):
  File "/tmp/rlocktesting.py", line 28, in <module>
    lock.acquire()#global scope
    ~~~~~~~~~~~~^^
KeyboardInterrupt

I excpeted main thread to successfully acquire lock twice (first inside SIGALRMhandler, second continuing inside global scope)

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions