Sandra Bullock's worst nightmare -- the guy she found standing in her hallway in the middle of the night -- copped a plea in the felony stalking and burglary case ... he won't be going to jail, but the punishment is actually comforting to Sandra.

The judge gave Joshua Corbett 5 years of probation and ordered him to continue getting in-patient treatment at a mental health facility. Corbett broke into Bullock's home back in 2014 ... and she actually came face-to-face with him after she was awakened by a loud noise.

Bullock made an understandably panicked 911 call after barricading herself in her room ... which was played in court.

Here's why probation is comforting to Bullock ... up until now, all she had was a restraining order which does no good if the person restrained has mental issues and doesn't take his or her meds. No one's monitoring the person who's the subject of the restraining order.

Now that Corbett is on probation, he'll be monitored by a probation officer who can keep tabs on him and make sure he takes his meds. Typically, someone who takes the required meds becomes harmless.

For safe measure, the judge ordered Corbett to stay clear of Sandra for 10 years.

SCARY 911 CALL