FOUNDING PRICING · 40% OFFfor the first 1,000 members · from ₹95/month
OutageNASA JPL11 min read

Mars Pathfinder Priority Inversion: The Bug Fixed From Earth

Why the Mars Pathfinder lander kept resetting in July 1997: a priority inversion on a VxWorks mutex, how JPL reproduced it, and the one-flag fix sent to Mars.

ShareXLinkedInWhatsApp
The Sojourner rover sitting on a solar panel petal of the Mars Pathfinder lander, with deflated airbags around it and a rocky Martian plain stretching to low hills on the horizon.
The Sojourner rover sitting on a solar panel petal of the Mars Pathfinder lander, with deflated airbags around it and a rocky Martian plain stretching to low hills on the horizon. Photo: NASA/JPL, Public domain, via Wikimedia Commons.

Mars Pathfinder landed on 4 July 1997 and began sending back pictures and weather data. A few days later its computer started resetting itself, each time throwing away the rest of that day's planned work. The cause was priority inversion: a high-priority task was stuck waiting for a lock held by a low-priority task that could not get the CPU. The engineers at NASA's Jet Propulsion Laboratory reproduced the fault on Earth and fixed it by switching on one option in the operating system.

Every operating systems course mentions this incident in a sentence or two. The details are worth more than that, because the man who led the flight software team published them, and they show how a correct lock, a correct scheduler and a correct safety check can combine into a failure.

A timeline of one failed bus cycle on Mars Pathfinder with four rows for task priority: the low-priority ASI/MET task takes a mutex inside select and is preempted, medium-priority tasks run, the high-priority bc_dist task wakes and blocks on the same mutex, more medium-priority tasks run, and the highest-priority bc_sched task wakes, finds bc_dist unfinished and resets the computer.

What went wrong on Mars

The lander's computer ran its software on VxWorks, a commercial real-time operating system from Wind River Systems. Soon after the lander began gathering meteorological data, it started to suffer total system resets.

A reset reinitialised all the hardware and software and stopped whatever sequence of commands the ground had sent for that day. According to the flight software lead, Glenn Reeves, data already collected was kept as long as power was not lost, but the remaining activities for the day were not done until the next day. On a mission whose surface time was limited, each reset cost real science.

Two public accounts exist, both from December 1997. The first is a summary by Mike Jones of a conference keynote given by David Wilner, the chief technical officer of Wind River. It spread widely and is the version most textbooks repeat. The second is a reply from Reeves, who corrected several details. Where they differ, this post follows Reeves and says so.

WhenWhat happened
4 Dec 1996Mars Pathfinder is launched from Cape Canaveral.
Before landingThe team sees the problem in testing but cannot make it repeat. Entry and landing software takes priority.
4 Jul 1997Landing in Ares Vallis.
A few days laterMeteorological data collection is under way and the resets begin.
Within 18 hours of tryingThe failure is reproduced in the lab with tracing switched on.
After testingA patch enabling priority inheritance is sent to the spacecraft. The resets stop.
27 Sep 1997Last data transmission from the lander.
7 and 15 Dec 1997Jones's summary and Reeves's reply are circulated.

The sources do not give the calendar dates of the individual resets or of the patch upload.

Technicians in white clean room suits standing around the Mars Pathfinder lander on a work stand, with its three solar panel petals partly open and the small Sojourner rover fixed to one of them.
Jet Propulsion Laboratory workers closing the petals of the Mars Pathfinder lander before launch, October 1996. The Sojourner rover is on the petal facing the camera. Photo: NASA, public domain, via Wikimedia Commons.

How the lander's software was organised

A single CPU controlled the spacecraft. Many of its instruments were reached over a 1553 data bus, including the accelerometers, the radar altimeter and the weather instrument, known as ASI/MET. The bus interface hardware was inherited from the Cassini spacecraft and was designed to be driven at 8 Hz, so everything on the bus worked in cycles of 0.125 seconds.

Two tasks ran that cycle:

  • bc_sched, the bus scheduler, set up the transactions for the next cycle. It had the highest priority of any application task.
  • bc_dist, the distribution task, collected the results of the transactions and handed the data to whoever needed it. It had the third-highest priority. The task for entry and landing sat between the two.

General spacecraft tasks had lower priorities than these. Science tasks such as imaging, image compression and the ASI/MET task were lower still.

VxWorks uses preemptive priority scheduling. The highest-priority task that is ready to run gets the CPU, and it takes it at once from any lower-priority task.

Each cycle, bc_sched and bc_dist each checked that the other had finished its work. Reeves describes the completion of bc_dist before bc_sched starts as a hard deadline. If bc_sched woke up and found bc_dist still unfinished, the software treated that as a serious fault and reset the computer.

A diagram of one 0.125 second cycle of the 1553 bus: the bus is active from t1 to t2, bc_dist runs from t2 to t3 distributing the data, bc_sched runs from t4 to t5 setting up the next cycle, and a marked check at t4 shows that bc_dist must have finished before bc_sched starts; below, bc_dist sends data to most tasks through shared memory but to the low-priority ASI/MET task through a pipe whose select wait list is guarded by a mutex without priority inheritance.
One 8 Hz bus cycle and its hard deadline

Most tasks read bus data from shared memory. The ASI/MET task was the exception. It received its data through an inter-process communication mechanism built on VxWorks pipes, and it waited for messages with select().

The root cause: three priorities and one mutex

Inside VxWorks, select() keeps a list of the descriptors that tasks are waiting on. That list is shared, so it is protected by a mutual exclusion semaphore, a mutex. Both ends of the pipe touched it: the ASI/MET task when it called select() to wait, and bc_dist when it wrote new data into the pipe.

Reeves gives the exact sequence.

  1. The ASI/MET task, low priority, called select() and was holding the mutex. It was in the act of releasing it.
  2. Before the release completed, the scheduler preempted ASI/MET in favour of medium-priority tasks. That is normal behaviour.
  3. bc_dist woke up for its cycle and tried to send the latest ASI/MET data through the pipe. The write needed the mutex, which was still held, so bc_dist blocked. Also normal: that is what a mutex is for.
  4. With bc_dist blocked, the scheduler picked the highest-priority ready task. That was again one of the medium-priority tasks, not ASI/MET. So the one task that could release the mutex did not run.
  5. bc_sched woke up on schedule, saw that bc_dist had not finished, and declared the error that reset the computer.

No single step is a bug. The lock worked. The scheduler obeyed its rule. The deadline check did its job. The fault is in the combination: a high-priority task was made to wait, not just for the short critical section of a low-priority task, but for every medium-priority task that happened to be ready. The medium-priority tasks had nothing to do with the mutex, yet they were effectively running ahead of bc_dist. That reversal is what the name priority inversion describes.

Jones's summary tells the same story with different actors: an "information bus" in shared memory guarded by a mutex, a low-priority meteorological task, a medium-priority communications task, and a watchdog timer that fired. The mechanism is identical. The specific tasks and the select() mutex above are from Reeves's correction.

How the team found it

A reset that happens occasionally on another planet cannot be debugged by looking at it. The team needed to see the order in which tasks ran.

The flight software had tracing built in. It recorded events such as context switches, semaphore operations and interrupts into ring buffers, and could dump them on request. On the spacecraft the tracing was normally off, because it produced more data than the radio link could carry. But the code was still there, since the team's rule was to fly exactly the software it had tested.

On Earth, engineers ran the same software in the lab with tracing on and tried to recreate the conditions. Reeves says they made the failure happen in under 18 hours. Conveniently, bc_sched had already been written to stop the trace and dump it when it detected its error. Reading the trace made the inversion obvious.

The fix: one option, changed remotely

The standard cure is priority inheritance. When a high-priority task blocks on a mutex, the task holding the mutex temporarily runs at the blocked task's priority. On Pathfinder that would let ASI/MET outrank the medium-priority tasks for the few instructions it needed to release the mutex. Then it drops back, bc_dist takes the mutex and finishes in time.

VxWorks mutexes support this as an option chosen when the mutex is created. In application code it looks like this (an illustration of the VxWorks API, not the flight code):

/* Without priority inheritance */
m = semMCreate(SEM_Q_PRIORITY);

/* With priority inheritance */
m = semMCreate(SEM_Q_PRIORITY | SEM_INVERSION_SAFE);

The mutex in question was created inside the operating system's select() library, not in JPL's code, and by default it did not have inheritance enabled. Wind River kept the creation options in a global variable. Changing that variable would affect every select() mutex created afterwards, not only the one on the ASI/MET pipe.

So the team did not simply flip it. Reeves lists their questions: what else would change system-wide, how much performance would be lost, and whether select() itself would behave differently. Wind River's analysis was that the performance cost would be minimal and behaviour would not change, provided only one task waited on any given descriptor, which was true on Pathfinder. JPL confirmed this with its own tests before sending anything.

The change went up through the mission's normal patching process: the differences between the on-board software and the corrected version were transmitted, and software on the spacecraft validated and applied them. Jones's summary says a short C program was run through the VxWorks interpreter. Reeves says the shell was available but was not used for this.

Two timelines compared: without priority inheritance the low-priority ASI/MET task holds the mutex while medium-priority tasks run, bc_dist stays blocked and misses its deadline, and with priority inheritance ASI/MET is raised to the priority of bc_dist as soon as bc_dist blocks, releases the mutex at once, and bc_dist finishes before bc_sched checks.
The same moment without and with priority inheritance

Why it was not caught before launch

Reeves answers this directly.

The inversion needed two things at once: ASI/MET data being collected, and the medium-priority tasks being heavily loaded. Pre-launch testing covered what the team thought was the best case for data rates and science activity. On Mars the link performed better than expected, more science was scheduled, and the load went beyond anything tested.

The team had seen the reset before landing and could not reproduce it. With limited time, they put the entry and landing software first, because a failure there would end the mission, while a reset on the surface was recoverable. The system had been designed to survive resets, even during atmospheric entry.

On one point the two accounts disagree. Jones's summary says priority inheritance had been judged too costly for a time-critical path. Reeves says there was no deliberate decision: the option inside select() was simply missed. His stated lesson is to understand how commercial software really works before flying it.

The computer science underneath

Mutual exclusion. Shared data that more than one task modifies needs a lock so that only one task is inside the critical section at a time. Mutexes, semaphores and the critical section covers why. A mutex makes one task wait for another by design, and that is where it meets the scheduler.

Priority scheduling. A preemptive priority scheduler always runs the highest-priority ready task, as explained in priority scheduling and multilevel feedback queues. The key word is ready. A task blocked on a mutex is not ready, so its priority counts for nothing until the holder lets go.

Priority inversion and its cures. If only two tasks are involved, the high-priority one waits for the length of one critical section, which is bounded and usually tiny. Add a third task at a middle priority and the wait is unbounded. The lesson on priority inversion and lock convoys works through this and the two classic remedies: priority inheritance, and the priority ceiling protocol, where a task taking a lock is raised straight to the highest priority of any task that could ever want it. The inheritance protocol was formalised by Lui Sha, Raj Rajkumar and John Lehoczky in a 1990 paper. Jones records that the three were in the audience at Wilner's keynote.

Hard deadlines. In a real-time system, a late answer is a wrong answer. Real-time scheduling: rate-monotonic and earliest deadline first shows how to prove that periodic tasks meet their deadlines, and that analysis must include blocking time on locks. Real-time and embedded operating systems describes the kind of kernel VxWorks is.

What an interviewer could ask from this

What is priority inversion? A high-priority task waits on a resource held by a low-priority task, while medium-priority tasks that do not need the resource keep the holder off the CPU. The high-priority task is in effect delayed by lower-priority work.

Why does it need three priority levels? With two, the wait is one critical section. The third, middle level is what makes the delay unbounded, because it can preempt the lock holder for as long as it has work.

How does priority inheritance fix it, and what does it cost? The lock holder temporarily runs at the priority of the highest task it is blocking, so middle tasks cannot preempt it. The cost is extra bookkeeping in the kernel on lock and unlock, and it does not prevent deadlock.

How is the priority ceiling protocol different? Each lock has a ceiling equal to the highest priority of its users. A task holding the lock runs at the ceiling straight away, without waiting for someone to block.

Was this a deadlock? No. Nothing was waiting in a cycle. ASI/MET would have released the mutex as soon as it ran. It was starved, not deadlocked.

Why reset the whole computer over one late task? In a hard real-time system a missed deadline means the system's state can no longer be trusted. A reset to a known good state was the designed, safe response, and the system was built to recover from it.

Takeaways

  • A correct lock and a correct scheduler can still fail together. Analyse them as one system.
  • Any mutex shared between tasks of different priority in a real-time system needs priority inheritance or a ceiling, including mutexes hidden inside libraries you did not write.
  • Timing bugs depend on load. Test beyond the best case you expect.
  • Keep tracing and diagnostic code in the shipped software. It turned an intermittent reset on another planet into a readable trace.
  • An unexplained failure in testing that will not repeat is still a real bug waiting for the right conditions.

Sources

  1. Glenn Reeves (JPL): What really happened on Mars? The authoritative account, 15 December 1997
  2. Mike Jones: What really happened on Mars? (summary of David Wilner's RTSS keynote), 7 December 1997
  3. NASA Science: Mars Pathfinder mission page

Written by the Codevizag Academy team

An independent explainer built from the public sources listed above. Codevizag Academy is not affiliated with NASA JPL. Found a mistake? Write to support@codevizag.com.

More case studies

Understood it? Now prove it.

The lessons behind this case study are free, and 20 problems a month are judged on real test cases at no cost.

Start solving free