Swift Timer doesn't work on Linux. Works on macOS

52 Views Asked by At

I noticed an issue with Swift's Timer, and I'm not sure if this is a Swift problem, a problem with a library I'm using, or a problem with my code. I have the following Timer:

var updateTimestampTimer = Timer(timeInterval: 15, repeats: true) { timer in
     print("updating timer")
     self.updateTimestamp()
}
RunLoop.main.add(updateTimestampTimer, forMode: .common)

When I run the code on macOS, I get the expected output ("updating timer" printed to the console every 15 seconds), but when I run it on Linux (Ubuntu 22.04), nothing is printed to the console. I'm also experiencing this issue with Timer.scheduledTimer and DispatchSource.makeTimerSource. Here's the full code, none of the three timers I created are firing:

DispatchQueue.main.async {
            let removalTimer = Timer(timeInterval: 120, repeats: true) { timer in
                if self.users.isEmpty {
                    self.group.connectedMembers = []
                    self.readyToRemove = true
                }
                self.updateGroup()
            }
            RunLoop.main.add(removalTimer, forMode: .common)
            print("creating timestamptimer")
            var timestampTimer = Timer(timeInterval: 1, repeats: true) { timer in
                if self.group.playbackState != nil {
                    if self.group.playbackState!.state == .play {
                        self.group.playbackState!.timestamp += 1
                        print(self.group.playbackState?.timestamp)
                        if self.group.playbackState!.timestamp >= self.group.currentlyPlaying!.duration {
                            //next song
                            self.group.playbackState!.timestamp = 0
                            self.nextSong(sentBy: SQUser(id: "0", username: "dfsfdsfg"))
                        }
                        
                    } else {
                        print("paused")
                    }
                } else {
                    print("playback state nil")
                    
                }
            }
            RunLoop.main.add(timestampTimer, forMode: .common)
            var updateTimestampTimer = Timer(timeInterval: 15, repeats: true) { timer in
                self.updateTimestamp()
            }
            RunLoop.main.add(updateTimestampTimer, forMode: .common)
            print("created tiemstamptitmer")
        }

Here is a list of packages I'm using:

  • Vapor v4.89.0
  • Fluent v4.0.0 (paired with FluentSQLiteDriver v4.0.0)
  • Leaf v4.0.0

I tried using the Jobs package as an alternative, and it did fire, but it also seemed to crash the server with error Illegal Instruction, so that gets us nowhere.

The server is running Swift version 5.10 on Ubuntu 22.04.4 LTS with 1 CPU Core, 25 GB Storage, 1 GB RAM (from Linode)

0

There are 0 best solutions below