-
Notifications
You must be signed in to change notification settings - Fork 13
EXC_BAD_INSTRUCTION in status-getter because of negative UInt64 #8
Description
Hi,
while accessing the status-getter of Health a EXC_BAD_INSTRUCTION crash is possible, because the line Date.currentTimeMillis() - self.lastStatus.tsInMillis in Health.swift
It is possible that self.lastStatus.tsInMillis is > Date.currentTimeMillis(), which would create a negative number.
Why is it possible that self.lastStatus.tsInMillis > Date.currentTimeMillis():
The rounding behaviour from DateFormatter.string(from date: Date) (which is used to create the timestamp of Status) and UInt64(date.timeIntervalSince1970 * 1000.0) (which is used in currentTimeMillis()) is different.
Example:
let date1 = Date()
print(date1.timeIntervalSince1970) // 1529757278.70769
let date1String = StatusDateFormatter().string(from: date1)
print(Status.dateFormatter.date(from: date1String)!.milliseconds) // 1529757278708
let date2 = Date()
print(date2.timeIntervalSince1970) // 1529757278.70794
print(UInt64(date2.timeIntervalSince1970 * 1000.0)) // 1529757278707
You can easily create a loop that runs this code after < 1second the milliseconds of date1 are larger than of date2.
Because it is is very common that health-checks run very often and from multiple sources, it is very likely that after a short time such situation happen. (In our situation every < 12h a server is crashing because of this)