Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1908

Foundation Thread exhaustion

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Foundation
    • Labels:
    • 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.

      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:

      import Foundation
      import Glibc
      
      var counter = 0
      while true {
        sleep(2)
        counter = counter + 1
        let t = NSThread(){
          print("STARTED:\(counter)")
          sleep(1)
          print("EXIT:\(counter)")
          NSThread.exit()
        }
        print("START:\(counter)")
        t.start()
      }
      

      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:

      START:231
      STARTED:231
      EXIT:231
      START:232
      STARTED:232
      EXIT:232
      START:233
      STARTED:233
      EXIT:233
      

      After this subsequent calls to thread.start() return silently given that Thread.start() has no return value:

      START:234
      START:235
      START:236
      START:237
      START:238
      START:239
      START:240
      START:241
      START:242
      START:243
      

      An x86 server equivalent suffers the same fate but in hours rather than minutes.

        Attachments

          Activity

            People

            • Assignee:
              joeiachievedit Joe
              Reporter:
              joeiachievedit Joe
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: