New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-1908] Foundation Thread exhaustion #4560
Comments
Interesting, Thread.exit() just calls to pthread_exit; I wonder if there is some other sort of underlying failure there. |
I don't have a pi handy to test this out; but the one differential between the Darwin implementation of the objective-c version and the swift-corelibs-foundation version is that the thread attributes for the objective-c version has a few initializers pthread_attr_init(&ivars->attr);
pthread_attr_setscope(&ivars->attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&ivars->attr, PTHREAD_CREATE_DETACHED); |
Comment by Joe (JIRA) @phausler: I believe I can patch these initializers in and kick the tires. I'll provide the patch that I came up with and report back. |
Comment by Joe (JIRA) @phausler: I applied the general patch into References: You can think of joinable threads as akin to child threads. Although they still run as independent threads, a joinable thread must be joined by another thread before its resources can be reclaimed by the system. Joinable threads also provide an explicit way to pass data from an exiting thread to another thread. Just before it exits, a joinable thread can pass a data pointer or other return value to the pthread_exit function. Another thread can then claim this data by calling the pthread_join function." |
Comment by Joe (JIRA) Fix looks good. Shortened delay of creation/destruction of threads: import Foundation
import Glibc
var counter = 0
while true {
usleep(100)
counter = counter + 1
let t = Thread(){
print("STARTED:\(counter)")
usleep(50)
print("EXIT:\(counter)")
Thread.exit()
}
print("START:\(counter)")
t.start()
} This is running steady state on a Pi 3 with over 1 million threads created and destroyed. That was compared to ~250 without the patch. |
Comment by Joe (JIRA) #425 PR submitted. Pi 3 at 1.7M thread creates/destroys. |
Comment by Joe (JIRA) Merge complete, will rebuild, verify, and close. |
Comment by Joe (JIRA) Verified fixed against e74f745 Closing. |
Environment
Thread exhaustion observed on a Raspberry Pi 3 running Xenial Xerus. If anyone needs assistance/login to a Pi3 to run and observe please let me (joe@iachieved.it) know.
Additional Detail from JIRA
md5: 656708644b9554713d9e2484ece916f0
Issue Description:
I discovered this while working with a Raspberry Pi 3, as the current implementation of
Thread
exhausts much faster on it than compared to, say, an x86 server with 16G of RAM.Consider the following:
The
sleep(2)
is designed to space thread creation out to such that only one thread is running at a time. On a Raspberry Pi 3 this code makes it this far:After this subsequent calls to
thread.start()
return silently given thatThread.start()
has no return value:An x86 server equivalent suffers the same fate but in hours rather than minutes.
The text was updated successfully, but these errors were encountered: