View Issue Details

IDProjectCategoryView StatusLast Update
00011141003.1(2016/18)/Issue7+TC2System Interfacespublic2024-06-11 09:09
ReporterFlorian Weimer Assigned To 
PrioritynormalSeverityEditorialTypeClarification Requested
Status ClosedResolutionAccepted As Marked 
NameFlorian Weimer
OrganizationRed Hat
User Reference
Sectionfork
Page Numberunknown
Line Numberunknown
Interp Status---
Final Accepted Text0001114:0004055
Summary0001114: Clarify if fork preserves thread resources
DescriptionThe current specification says this:

“If a multi-threaded process calls fork(), the new process shall contain a replica of the calling thread and its entire address space, possibly including the states of mutexes and other resources.”

I find it pretty clear that thread stacks and TLS variables have to be replicated in the new process as well, for all threads (not just the current thread), at least for objects whose address has been published to the current process.

However, there is some debate whether the current wording allows deallocation of thread stacks in the new process at fork time.
Desired ActionClarify that all thread resources are replicated, perhaps like this:

“the calling thread and the entire address space of the parent process”

And add a new sentence: “Addressable thread-specific resources, such as objects declared on the stack and TLS variables, are replicated for all threads, not just the current thread. The fork function call does not cause any threads to exit.”

The lack of thread exit means that destructors registered with pthread_key_create are not called.

Related glibc bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19430
Tagstc3-2008

Relationships

related to 0001383 Closed Issue 8 drafts Make "Application Usage" less confusing. 

Activities

torvald

2017-01-12 16:50

reporter   bugnote:0003541

The alternative perspective is that things like on-stack data belong to a particular thread, and are bound to the lifetime of the thread. Therefore, if fork() replicates just the calling thread but not all other threads that may exist, the stacks of those other threads also do not need to be replicated; they would have no owner.

geoffclare

2018-07-19 15:20

manager   bugnote:0004054

This was discussed in the July 12 and July 19, 2018 teleconferences. We believe no change is needed to the normative text of the standard. It clearly states that the child contains a replica of the calling thread's entire address space. The request to change this to specify the process's address space would make no difference since the address space of the thread and the process are one and the same. This is clear from XBD 3.404 Thread:

"Anything whose address may be determined by a thread, including but not limited to static variables, storage obtained via malloc(), directly addressable storage obtained through implementation-defined functions, and automatic variables, are accessible to all threads in the same process."

The standard is also clear that a process is created with a single thread. Any additional threads in the parent simply don't exist in the child; they aren't terminated because in order to be terminated they would first have to exist.

geoffclare

2018-07-19 15:22

manager   bugnote:0004055

On page 899 line 30368 section fork(), change APPLICATION USAGE from:
None.
to:
When a multi-threaded process calls fork(), although all memory that is addressable in the calling thread remains addressable in the child, there is no guarantee that the contents of thread-specific memory, such as stacks or thread-local storage, that was specific to other threads in the parent will still be the same in the child. This is because such memory could be treated as available in the child given that the threads it related to do not exist in the child, and consequently the processing performed in the child before fork() returns could make use of that memory. Therefore applications should avoid using in the child any pointers to thread-specific memory that were passed to the calling thread from other threads in the parent.

Issue History

Date Modified Username Field Change
2017-01-12 16:05 Florian Weimer New Issue
2017-01-12 16:05 Florian Weimer Name => Florian Weimer
2017-01-12 16:05 Florian Weimer Organization => Red Hat
2017-01-12 16:05 Florian Weimer Section => fork
2017-01-12 16:05 Florian Weimer Page Number => unknown
2017-01-12 16:05 Florian Weimer Line Number => unknown
2017-01-12 16:50 torvald Note Added: 0003541
2018-07-19 15:20 geoffclare Note Added: 0004054
2018-07-19 15:22 geoffclare Note Added: 0004055
2018-07-19 15:23 geoffclare Interp Status => ---
2018-07-19 15:23 geoffclare Final Accepted Text => 0001114:0004055
2018-07-19 15:23 geoffclare Status New => Resolved
2018-07-19 15:23 geoffclare Resolution Open => Accepted As Marked
2018-07-19 15:23 geoffclare Tag Attached: tc3-2008
2019-10-30 10:34 geoffclare Status Resolved => Applied
2020-07-27 09:07 geoffclare Relationship added related to 0001383
2024-06-11 09:09 agadmin Status Applied => Closed