Why do fans spin backwards slightly after they (should) stop?

The overshoot behavior you noticed is called cogging and occurs when the magnet arrangement in the motor "catches" the rotating magnetic core of the motor during shutdown and jerks it back to one of the local strong spots in the field.

You can demonstrate this yourself by carefully rotating the fan blade around with your finger when the motor is off. You will notice there are certain rotation angles where the fan wants to come to rest and others which it wants to avoid. If the motor passes one of the preferred spots but fails to rotate far enough to "climb the hill" and snap forward into the next cog spot, the motor will very briefly rotate backwards a fraction of a turn and go "boing-oing-oing-oing" as it settles into that cog position.

Your PC is a box with limited vents for air to enter/exit. Especially if these clog up with dust, the fan could create a noticeable pressure differential between inside and outside of the box. After the fan turns off, that equalizes, forcing some air backward through the fan, and causing it to rotate backward. Maybe.

Another possibly, as pointed out in comments, and since you report much less than a full rotation backward, is that the DC motor running the fan likes to be in certain orientations and not others when it is unpowered, due to alignment of the permanent magnet with other motor parts. As the fan slows down, at some point it passes one of these preferred orientations but lacks the momentum to make it over the hump to the next one, and rotates back.

On balance, I think @nielsneilsen's answer is likely the correct one. But there is another possibility.

Is there a control loop in play?

Almost universally, practical control systems for hitting a desired position, velocity, flow rate or whatever use a PID control loop, sometimes omitting the P or D element depending on the application. Other control techniques exist, but a PID controller has a number of advantages.

  • It's very well understood.
  • It's very simple to configure.
  • It hits the target value without much error.
  • It doesn't need a particularly complicated implementation, and can even be built with old-school analogue electronics if you want.
  • It remains stable over a fairly wide range of actuator/sensor tolerances, so you don't need to tune each widget individually, making production easy.
  • The ramp-up/down time is easily configurable, which can be useful where (as with a motor) a rapid change of output produces a current spike.

One standard feature of a PID controller though is that in order to get the position/velocity/whatever to settle rapidly to where you want, you have some overshoot.

PID examples from Wikipedia

If you have a speed controller on the motor, and it uses a PID loop to govern speed, then stopping the motor will naturally give a small speed in the opposite direction before it comes back to zero.