Infinite loop in macOS Night Shift in the summer near the Arctic Circle 588 points by guessmyname on June 28, 2019 | hide | past | favorite | 164 comments

 Deep in the twitter replies:> Now, is this a corner case, an edge case or a boundary case? And does the answer depend on whether you are a flat-earther or not?That was too funny.
 Personally I believe that earth is flat disk carried by 4 elephants, which are standing on the back of a giant turtle
 You’re wrong. I know for a fact that there are 7 elephants, one of them is white.
 Sure, but that turtle is carried by four "elephants, which are standing on the back of a giant turtle". And that turtle ...
 The Hindu sees a recursive algorithm. The flat earther cant see past the stack overflow.
 I really wish people would find true enlightenment. It's obvious a turtle in space wouldn't need anything underneath -- because turtles swim!
 Sure. But really, it's all illusion. So better just unask the question.
 C'mon, everyone knows its just turtles, all the way down
 Are you sure it's only four? Because what happens when you have over four billion elephants? Is there a potential overflow issue here?
 It's turtles all the way down.
 The Turtle Moves
 Wouldn't we have eight seasons then?
 Topology will finally have a practical application.
 Semi-related, I noticed that Google handled this case in the weather part during my last year's summer visit of Utqiagvik, AK: https://twitter.com/fishywang/status/1013931269363658754
 Perhaps it's because they implemented the "Sunrise equation" naively, which becomes indeterminate for high latitudes when the Sun is close to the maximum?https://en.wikipedia.org/wiki/Sunrise_equationcos(\Omega) = - tan(\phi) x tan(\delta)Where \Omega = hour angle (sunrise, sunset offset from noon), \phi = latitude, \delta = sun declination.Clearly if the right hand side gets big enough (>1), this doesn't work.
 Never knew about that equation! That's cool.Angles are always such a pain. In my experience it's worth applying trigonometric identities and working in terms of unit vectors, sines, and cosines instead of using angles directly. In this case angles seem unavoidable, though: acos(tan(x)*tan(y)) doesn't simplify to anything useful.
 It's omega = acos( - tan(x) * tan(y)), not that that makes much of a difference.
 My old Garmin eTrek something-or-the-other had a feature which would compute local sunrise and -set.It threw a fit and rebooted when I asked it for sunset while at 78 degrees north in June.
 This is a beautiful example of the importance of diversity on a team. I have no idea how diverse the team is and I am not speaking of racial or sex diversity... this is the core of the discussion, having people with varying life experiences prevent obvious bugs like this or innovative ideas that no one person could think about. This is a fun bug
 Whilst that’s true, I think it’s also a good example of how shortsighted most attempts at diversity are; in this case a Scandinavian would perhaps have the “diverse experience” to preempt a naïve implementation of a sunrise fiction, however diversity seems to always be about race or sex or identity, which seems to miss the point somewhat.Edit: Which is to say that a male with fair hair and skin and a university education wouldn’t qualify as diverse in most contemporary conceptions of diverse, despite a likely underlying diversity of culture and experience.
 Apple's diversity manager said something to that effect, and then she was made to apologize and leave.So, yes, diversity of experience is important, and no, that's not what is accepted as diversity in modern corporate culture.
 That episode proves the point I think. It seems to demonstrate perfectly how the only diversity that counts is race and sex. The article literally concludes with a breakdown of Apple’s workforce by ethnicity and sex.
 My thinking was rather that they made an assumption. Which is a much simpler explanation because people make faulty assumptions every day.But yeah, diversity would have helped.
 I believe the first portion of your reply directly supports the original comment here. Everyone's assumptions are based off of their experiences. A diversity of experiences and backgrounds creates an environment where assumptions are challenged more frequently. The more than a team can support their decisions through facts, instead of assumptions, the stronger their solutions will be.
 > I am not speaking of racial or sex diversityI was about to wonder how racial and sex diversity plays into a program running into an infinite loop.
 The perils of living on a spinning oblate sphere strike again!Software would be so much simpler on a ringworld ...
 Or an infinite frictionless plane in a vacuum
 Or a discworld, sitting on the back of four elephants who are standing on the back of a turtle.
 What does the turtle stand on?
 It floats in space. Let's not get crazy.
 It's turtles all the way down.
 The Turtle Moves
 This is due mainly to our tilt, not our sphere-ness
 On a spherical world with no tilt, what time is sunrise at the north pole?
 Approximately four billion years ago.
 Don’t you mean 6,000 years?
 Depends how tall you are.
 Depends on the angle of the ecliptic
 You're right, we need to straighten the earth up.
 Has anyone ever determined how many times you have to drive around Infinite Loop before Apple Security calls the police on you?
 I had a friend who once drove around a traffic circle as many times as he could. He got up to 39 times before a police officer pulled him over.Does anybody know if there is a law on that?
 Depends on the state/country, but most have rule that you can't go around more than once. In fact, most places I've seen in the US you can't even go more than 3/4s around (although I'm not sure if that's a law or just a posted suggestion).
 Doesn't appear to be the case in the UK at least:> It is illegal to circle a roundabout more than 3 times> FALSE> There is no evidence to support this, other than the 'careless driving' argument. You should plan before you enter a roundabout and circling more than twice could be considered as careless driving.https://www.endsleigh.co.uk/blog/post/false-driving-myths/
 What the hell? What if you miss your turning or want to do a u-turn? Going around more than once is a perfectly reasonable thing to do.
 What if it's a magic roundabout? [1]
 It looks like a pinball machine with bumpers and flippers!
 On a turbo roundabout, you can't.
 I was pulled over with a few friends after a few loops. One of the roundabout entrances was a hospital. I didn't get a citation of any sort, but the cop basically handwaived with "blocking traffic" (of which there was none).Basically, it depends how bored your local police are
 Some places have laws against "cruising"
 Less than one (source: I did it trying to find parking because the BJs next door was full)
 They’ve certainly gotten suspicious after the first two loops.
 reminds of the Computerphile Tom Scott video "The Problem with Time & Timezones"https://www.youtube.com/watch?v=-5wpm-gesOYand why I will never implement datetime myself
 Nobody ever got fired for using the provided datetime.
 What is the standard for using datetime libraries?Use the one that comes with each language, or use some UTC library?
 'Falsehoods programmers believe about time: - Every day has a sunrise and a sunset'
 Falsehoods programmers believe about time:The fundamental one is: "I'm so very clever, and I've dealt with dates my whole conscious life, I can probably think of all of the edge cases off the top of my head!"
 I think you're right, but that the tone I read in your words would prevent people from improving.Rather than "I'm so clever" it is more "this isn't complicated". One can argue they mean the same thing, but I'd say they mean different things if comparing to an outside scale. The solution to thinking you're so clever is to reduce your arrogance, but the solution to thinking problems are simple is to stop assuming any problem is simple.
 My favorite incorrect fact is that there are 24 timezones. In fact, the number of offsets from UTC fluctuates throughout the year (at least last I worked on scheduling in 2012. Back then it was either 39 or 40 depending on DST)
 DST rules are just insane. I'm responsible for some code where I just drew the line and 40 some odd people in one of the Antarctic stations are just going to have to live with wrong time part of the year if they ever run my code. I'm not adding another code path and test case just because these people wanted their own special concept of time. I don't remember the pattern, but I think it was maybe a daylight savings time and then an extra one stacked in the middle.
 We have endless problems with DST as well. I can't wait for the EU directive that abolishes it. Russia dropped it years ago. We'll still have to support past times and other timezones though.
 I wonder how long time it will take to forget DST if it is abolished. Will that messy spaghetti code be ripped out and never seen again.
 Most standard libraries still have support for the Julian calendar. That stuff is never going away.
 Julian calendar is still used by the US Navy. Every supply department office you go into has a conversion table up on the wall.
 I doubt it's "most". Python, Javascript, and C++ don't seem to. The only ones I found that do seem to are C# and Java.
 I just got a ruby gem from someone who actually made tables on the stuff. I made a script that cut out far past and future times to lower loading time from 10m to 30s and figured I could upgrade the gem to leverage an expert’s research as timezones changed (e.g. India changing zones)
 Yeah I always feel like a jerk when people open tickets in the summer saying “Please do this at #:## EST” and I have to reply “thanks for providing a time but it’s currently EDT in NYC. Is that what you meant?”
 No reason to be snarky with people. They mean whatever time it currently is or will be.
 Are you sure though?If they copied that date from some automated system it might as well be any of these.Humans usually say “New York time”.
 In my experience, humans never say "New York time", or "San Francisco time", or anything like that, they say "EST" and "PST" regardless of whether daylight saving's is on.I'm guilty of this too, since I can never remember which part of the year constitutes "on" and which is "off", only where the boundaries are. If I stop and think I can usually remember "spring ahead is turning it on", but never off the top of my head.
 That's only slightly worse than how I feel when people say that something's happening in the summer when it's actually winter where I live.
 I hear you. It almost always is what people meant but most of us (myself included) rarely think about the distinction.
 I've just gotten to saying ET (or equivalent) all year-round .
 That’s the battle I fight: persuading people that saying “Eastern” is accurate and sufficiently precise. Precise and inaccurate is a bad combination.
 That becomes fairly obvious when you consider Arizona doesn't observe DST, but Utah is the same longitude (lies directly north) and does observe DST.
 Yep. And there also small regions that observe daylights savings WITHIN regions that don’t observe daylight savings WITHIn regions that do observe it. For example, in Indiana it is on a per county basis[1].
 Speaking of within, I fibbed slightly about Arizona.https://en.wikipedia.org/wiki/Time_in_ArizonaThe Navajo Nation is within Arizona's boundaries, and it does observe DST. And the Hopi Reservation is within the Navajo Nation, and it does not observe DST.And since Arizona is surrounded by California, Nevada, Utah, New Mexico, and Mexico, all of which do observe DST, that means you have an island of no-DST (Hopi) inside an island of DST (Navajo) inside an island of no-DST (Arizona) inside an area of DST (adjoining US states plus Mexico).
 How did we screw up time zones so much?
 We invented and then implemented time zones and then invented and then implemented daylight savings time; arbitrarily and with arbitrary laws and treaties.There are days where I think I should go back to a mechanical watch and adjust the time each day at high noon. It won’t agree with anyone else’s time most of the time, but at least it will be correct.
 But won't it be wrong as soon as you start travelling east or west, though?
 Well that's almost enough to make one think daylight saving time is a bunch of cracked-up nonsense!
 Somewhere I have an old book which includes time zone maps for Indiana in the 50s and 60s. Every single year there were changes.
 There's also half hour timezones, found at least in India, and I think a couple quarter hour zones.
 And that it's possible for two places to be more than 24 hours apart.
 but that the tone I read in your words would prevent people from improvingAre people really that fragile? Yes and no.The solution to thinking you're so clever is to reduce your arrogance, but the solution to thinking problems are simple is to stop assuming any problem is simple.Isn't humility the answer to both? Someone should develop a humility course for programmers involving an "ultra drone," writing the date/time library for it, and some simulated world test cases. Such "humility exercises" are found in various disciplines. There's one for CIA analysts, involving analyzing US Civil War sites via sat photos, then having them visit in person.
 > Are people really that fragile?It's not fragility, so much as perspective.When you are thinking something is simple, and you say "Am I'm thinking I'm impressed with my own cleverness?" it's easy to say "no". Because it's not arrogant to think you can accomplish something simple.When you are thinking something is simple, and you say "Am I assuming simplicity?" it's easy to say "yes".Realizing you're committing the fallacy is the first step to avoiding it.
 Right? I am reliably put into this weird emotional state of being simultaneously entertained and chagrined at the propensity for technologists (and I very much include myself in this criticism) to make these kinds of assumptions, and the edge-case bugs that fall out of them.
 If we tried to get these thing 100% right we'd not have time for anything else. Punting is pragmatic.
 You're absolutely right that "100% right" is an unreasonable standard. I submit that knowing that there are places on the planet where, at times, the sun doesn't set, however, is a reasonable thing to expect of someone who's made it far enough in life to be writing code (or testing it! How did this pass QA?) for Apple.f.lux gets it right if I set my zip to Barrow, AK. Why can't Apple, with how many orders of magnitude more resources at their disposal — including knowledge?
 Agreed
 And it's a reminder that "edge case" is relative. It's not an edge case for people who live there!
 It exists, it's just complex-valued.
 True that(Posted at (5+4i):(37+5.23812i) AM)
 Where does j & k fit into your quaternion representation?
 There are both coordinates and rotations, it would probably be better modeled as a quarternion or something of an even higher order.
 I believe you can actually compute the sunrise time of a location/day that doesn't have a sunrise, and it would be just a complex number (root of some analytic function).
 Aside from having a falsehoods checklist that includes this, how would this be caught by a programmer who isn’t mindful of the fact that you don’t always have a sunset?
 Consider the loop conditions (which are clearly non-trivial) and ask "is it certain that this loop will always terminate"?Presumably in this case you would have arrived at an answer along the lines of "yes, so long as there's always a sunset". Which should then prompt the question: hang on, is there always a sunset? A quick scan over whatever datafile it's using for input should answer that.
 Falsehood #2: Every day starts at 12:00 am midnight.
 Because there is neither a 12:00 am, nor a 12:00 pm. One is 12 noon, the other is 12 midnight.AM and PM are "ante meridian" and "post meridian": before" and after. But 12 is on* the meridian; neither one. You can meaningfully say 12:00:01 am, or 11:59:59 pm, but for 12 sharp nobody can say whether am or pm is dark or day.Airlines schedule flights at 12:01 for this reason.
 This is the reason 24hour clock is the way to go. 23:59, 00:00, 00:01, ...0 was invented for a reason. This am/pm stuff is so confusing.
 Meridian in this context is noon. 12 “anti meridian” is 12 hours before noon. 12am. Which is exactly correct.I’m no domain expert but I’m guessing flights are scheduled at 12:01am because people are bad at time. For instance some people would think 12am is the last minute of the previous day instead of the first minute of the next day. 12:01 removes the potential ambiguity.
 Nitpick but since we’re talking about time: AM means “ante meridiem”, as in “before”. Not anti as in “against” :)
 You've just completed GP's point perfectly though - 12 hours after noon (post meridian) is midnight too.
 12 "post meridian" is 12 hours after noon. 12pm. Which is midnight. But you don't use 12pm to mean midnight. Why not? Midnight doesn't actually belong to a day at all. Our current conventions of 12am and 12pm seem to be mostly products of digital time keeping.
 OU4483 leaves CPH at 1200, as does another flight.There isn't a flight at midnight, but there are fewer flights then anyway, and it may be confusing to some which day the flight is on. Of course, the flights around midnight have departure times of 2350 and 0015.
 Wot, what's the false case?
 DST. Some countries (Brazil) switch over at midnight instead of 2:00 am. One day of the year has no midnight.
 I never considered how many logical problems arise from the use of AM/PM. I knew time handling is tricky but it seems like every time I see this topic discussed, I learn about a new corner case.
 Some cultures end the day at (wait for it...) sunset.And now we're back to the original problem.
 Apparently Muslims and Jews here have agreed that the day ends at sunset or at 21:00, whichever comes first (or at all). It would be difficult if you couldn't eat for the better part of a month.
 credit to /u/gwern
 Time is hard: https://zachholman.com/talk/utc-is-enough-for-everyone-rightThis is all because of the fact that we're trying to represent the time taken by the earth (an arbitrary planet that has no impact on the universe whatsoever) to revolve around the sun (an arbitrary star) in terms of the amount of time the earth takes to rotate around itself. These two obviously unrelated things are somehow merged into to create an unsolvable clusterfuck. To make matters worse, let's sprinkle some politics...
 I was hoping that he'd cover the different kinds of time, and where each kind is used in a program, but nobody seems to think about it:* Absolute Time: A time value fixed to UTC.* Fixed Time: A time value fixed to a specific time zone.* Floating Time: A time value that is interpreted according to the time zone of the observer.
 > (an arbitrary planet that has no impact on the universe whatsoever)What do you call this type of worldview where you just view everything as completely meaningless or devoid of any value? Is it really straight up Narcism? I mean my god no wonder this is one of the most depressed generations on record.Apologies for the off-topic comment, I just felt compelled to post this. We are the most meaningful, most important things in the universe until we know or learn otherwise. We are the most intelligent, most accomplished life forms in our known existence. Viewing it as arbitrary might be "technically true" but then again what do we know? What if we are it? It is just as plausible as the other view: that we are just on a billion year course for the eventual heat death of the universe and the end of existence all together.Treat yourselves as special, you are. Treat others that way too. You'll also be a lot happier. Sorry for the platitudes.
 If I remember correctly the word you're looking for is nihilism.
 Ironically, viewing yourself as the most meaningful, important, intelligent, accomplished thing in the universe sounds a lot like narcissism.
 When your "universe" is the size of a solar system with a single life-supporting planet, what other conclusion could possibly be met?If you're the only one in a talent contest, you're the best ... and the worst.
 I like life, but we are meaningless motes in the universe. We'll all be dead one day and no one will remember us.It's ok though, it doesn't matter.
 If human matters do not matter, why assume that language carries meaning?Do you believe that animals have meaningful conversation? Or that humans do?Does the universe speak? If it did, would you know?Do you understand its language? Can you hear it?There's also something else: nothing.
 I've learned to enjoy the struggle and this has helped me immensely.
 It’s not an arbitrary planet from our perspective, which is the only one we even know to exist.We’ve needed to coordinate actions forever and the sun was the only reliable way to do that for a long time. Nothing arbitrary about where our time came from.
 Sure, for now; but I feel like it'd be aesthetically pleasing in some sense to try for something more objective—i.e. to predetermine the sort of timekeeping methods we would need upon establishing trade relations with a universe full of aliens, and then to use those methods in advance. Like a Society for Creative Anachronism meeting where the target period is forward in time rather than backward.
 Given that we have no idea whether aliens even exist — and that life on Earth both literally and figuratively revolves around the Sun — that strikes me as the ultimate premature optimization.
 If we ever meet aliens that also happen to be advanced enough to trade with, we will have already solved this issue ourselves because we’re cruising around in the Enterprise and needed a new timekeeping system by that point, or the Enterprise came to us and they will have their own timekeeping system that we can adopt.Before then? Eh. Objectivity is overrated and an imperfect time system is a nice reminder that life doesn’t revolve around us and what we want.
 This is true, depending on how far away you look from where you are.Or, this is true, if you allow that "day" is defined by sunrise to sunrise (or sunset to sunset, depending on your religion), which in the region discussed would give you one sunrise per revolution around the sun.Either one of those truths could be the basis for fixing this bug.EDIT: Rather than a bug (in the implementation), this may be better classified as an under-specification.
 I sometimes wonder how many bugs are lurking in systems using GPS for navigation at 180th meridian (where longitude goes from 180 to -180).
 I crossed the antimeridian several times on a trip last year. I usually carry a GPS logger on such trips, to contribute to OpenStreetMap.My USB logger was OK, but my backup logging apps both added crazy points around the world.Programs to display / edit the tracks also had bugs.Even Google Maps has defects on the maps at the antimeridian.
 IIRC there was a bug in an early version of a certain fighter jet where it would go inverted when crossing the equator. Oops.
 Urban legend or so it would appear.
 Oh, bummer. That would’ve been a fun case study if it had been true.
 When I was working on geo-fencing feature for a small UAV I did see the consequences of naive maths when working with coordinates near antimeridian (e.g. calculating a perpendicular vector of a fence segment).Also related- it seems crazy to me how some teams are working on a robotic product without building/interating a simulator first!
 The Suntimes Android app (its on F-Droid) correctly reports None for such high latitudes.
 Am I the only person who is impressed with this person's debugging skill?
 Not really.All he's shown is a behavior (high CPU) and is equating it with the latitude he's at. When he turns off Night Shift, the daemon process for the feature stops consuming as much CPU, from what I gathered. Nowhere does he provide evidence that there is an infinite loop calculating the sunrise/sunset.And I really don't want to be a stereotypical poster by simply degrading what he did, so I will give credit to him for knowing where to look and making a hypothesis that is not wrong. He's perfectly on point to _begin_ a debugging process, but I can't see he's found a bug. He's found an unexpected or undesired behavior. I certainly would consider this story/tweet as a huge plus if he were being interviewed by me for an engineering position, but it appears he's already well above my pay grade anyways.
 You’re splitting hairs and failing at it. They have clearly found a “bug” if Night Shift is consuming 100% CPU without end, even if their hypothesis for the reason might be wrong.
 >They have clearly found a “bug”...This depends on your definition of "bug". I think, in the classical sense, a bug is something that you understand both the cause and the resultant undesired behaviour and, as of consequence, you can reproduce the chain of events.Given that definition, I would be disinclined to agree with calling it a "bug".However, I would also like to point out that we're getting so nuanced in terms that we're, quite literally, dropping the notion of lauding the attempts of the OP to go down the road of wanting to deduce the problem, themselves.
 I disagree. If you have to understand the cause of undersires behaivor before a bug can be said to exist, then debugging is a prerequisite to a bug existing.If I write a program that computes 2+2 and it tells me the answer is 5, that’s a bug. Why is irrelevant until I want to fix it.
 As the OP, I'm giving this one an upvote. I am far from an engineer, so the most I can come up with a hypothesis. I need a real engineer to do the real debugging.
 A bit late, but I did want to point out that I enjoyed the article and found it engaging. I was not trying to diminish from you in any way. My comment was much more a semantical argument over the comment than a logical disagreement with your article.Anyways, thanks!
 What's the difference between unexpected and undesired behavior and a bug?
 Unexpected or undesired behaviours are relative to who's expectations and desires are being considered while bugs are (or at least should be) universally recognisable as bugs.
 Would anyone consider that the corebrightnessd process at 117% cpu is expected/desired behaviour?
 The machine gave two big hints. The fan spinning up indicated the computer was needlessly busy. Then the Activity Monitor revealed the problem was the "core brightness" system. From there it is a small leap to make the connection to Night Shift.On mobile devices the only indications we have that something is wrong is warm devices and lower battery lives, which are far less immediate. And we don't have the detailed CPU usage that desktop devices provide. Maybe we need a simulated fan sound when our devices are being pushed too hard.
 This Android phone tells me proactively if it notices an application using lots of battery life or network bandwidth. And if I'm inquisitive it will let me see how much they used anyway when it didn't alert.For example apparently Chrome used 3% of the battery life so far today whereas a game I was just playing already consumed 5%
 On Android you used to be able to open a terminal and run "top".With the improved security model, that now only shows the bash and top processes.
 You can get much more useful info out of Activity Monitor by running spindump.For iOS the Battery settings are usually good at telling you about power drain.
 I don't think the Battery settings shows you power usage by system daemons?
 It’s not specific, but it’ll say “System Services” I believe.
 Does anyone know how other similar programs behave in these latitudes? For example, f.lux, GNOME's night light, or Redshift?
 Reminds me of an early release build of Win95. It would properly set time back an hour for Daylight Saving Time, but not set a flag that it had been done, so it would do it over, and over, and over...
 now that's a great test case that could easily be tested using mocks.
 My favorite part of "To the Moon and Back" is when the NASA administrator is explaining to Congress that the three astronauts on board Apollo 1 died because "We weren't creative enough to imagine this could happen."
 Would this be a bug in the Night Shift feature, or something more fundamental, i.e. something in Apple's Foundation library?
 I wonder if Apple shares that code in iOS too?
 CoreBrightness is similar across the platforms.
 Maybe, but I don’t think there’s a CoreSunrise or CoreSunset. The problem isn’t in setting the brightness or colour tone, it’s calculation when to switch between them.
 iOS and macOS share GeoServices, which performs the actual calculation of sunrise/sunset times (among other things).
 hmm I wonder if it also applies in the other direction where the sun doesn't rise for a part of the year as well
 > where the sun doesn't rise for a part of the year as wellThat would be the same place a few months from now. This is strange though, it's not like this guy is the first above the polar circle using a Mac. I suspect it must somehow be related to him being a tourist with a different region and timezone settings or maybe the location services got confused.