It was May 17, 2009, and Astronaut Michael Massimino was lapping Earth at 18,000 miles an hour, sweating up his spacesuit as he struggled to fix the ailing Hubble Space Telescope. A stripped bolt was stopping him from removing a handrail to get at a crucial piece of hardware, and his nerves were fraying.

Massimino fumbled at the bolt repeatedly through thick gloves, but without luck. It seemed that one dumb piece of metal might stymie NASA’s billion-dollar rescue mission — but that’s not how things turned out. He finally managed to pry open the telescope and complete his job before clambering back inside the Space Shuttle Atlantis. Hubble returned to action, going on to snap some of astronomy’s most iconic photos.

Massimino was guided by experts on the ground, as astronauts always are. But his steely resolve in the face of long odds — and his methodical approach to solving a difficult problem while floating weightless in the vacuum of space — was honed by the brutal regimen of survival training he had endured more than a decade earlier.

Thrown to the elements

Being thrown into dangerous, demanding outdoor ordeals has been a key part of NASA’s astronaut training program for as long as there has been a NASA, going all the way back to the original Mercury 7 team.

In the early days, the agency plucked astronauts from a pool of test pilots who had already completed the military version of survival training. Former astronaut Jerry Linenger, a Navy pilot who in 1997 became the first American to spacewalk from Russia’s Mir space station, still vividly recalls what those trials were like.

One time, Linenger and three other aviators were dropped in a jungle in the Philippines with no supplies or instructions, aided only by a local guide who spoke no English. “We were out there for two-and-a-half days, and had no idea where we were,” he says. “The scariest part was nighttime — a cacophony of sounds, animals crawling underneath me.”

During the Apollo program of the late 1960s, NASA initiated rigorous outdoor exercises in Iceland, Hawaii and Arizona’s Meteor Crater. As the era of the International Space Station dawned in the late 1990s, NASA formalized its survival-training requirements, often partnering with the nonprofit National Outdoor Leadership School.

Now NASA astronaut candidates must complete two years of intensive preparation, including land and water survival exercises. The training begins before the candidates learn mission-specific skills — even before they’re accepted into the corps.

Though NASA’s survival training regimen has changed over the years, the goal remains the same. It’s not really about teaching astronauts specific skills to use in the face of life-or-death danger. NASA has underwater spacewalk exercises, flight simulators and T-38 training jets to impart those kinds of lessons.

As Massimino discovered during his Hubble near-disaster, NASA requires astronauts to go through survival training with a more subtle purpose in mind.

What’s the point?

A decade before his fateful day in orbit, NASA sent Massimino and four other newly minted astronauts on a 10-day wilderness expedition in the Canadian arctic. He was less than enthusiastic about the assignment. “We went to a training exercise in Cold Lake,” he recalls. “They gave it that name for a reason. It was really fricking cold!”

The men were given only basic survival gear — coats, hats, a few tools, navigation equipment and sleds — before a helicopter dropped them along an unfamiliar stretch of tundra. They pitched their tents, prepared food and hunkered down as temperatures plunged to minus 40 degrees Fahrenheit.

Mike Massimino, top right, with astronaut colleagues during extreme environment team building training with Canadian army instructors, bottom row, in Cold Lake, Canada in 2000. Courtesy of Mike Massimino

Daylight hours were short, making for a hectic schedule of breaking camp, moving to a new site and setting up again. The men had to haul everything themselves. The extreme cold made routine tasks like tying knots awkward, often infuriating.

One day while scouting a new campsite, Massimino got lost and spent hours walking in circles. Another time a leaky boot gave him frostbite that took months to heal. “That was a little uncomfortable,” he deadpans.

Bad as it was, Cold Lake wasn’t the worst survival training exercise Massimino had to face. That distinction may belong to a two-week canyoneering course that had him rappelling and scrambling about on high rock ledges in Canyonlands, Utah — “and I don’t like heights,” he adds.

Or maybe it was the time he was walloped by a sudden storm during a kayaking expedition in Alaska. “The sea got angry, we nearly capsized and I thought I was going to drown,” he says.

A chilly epiphany

Massimino, an outdoors-hating guy from Long Island, New York, initially cursed the exercises for their seeming pointlessness: They seemed far removed from the kinds of tasks that he would actually be doing in space. But halfway through his Cold Lake adventure, his perspective changed.

He recalls the epiphany in his 2016 book, "Spaceman: An Astronaut's Unlikely Journey to Unlock the Secrets of the Universe." Alone on the ice one night, mulling over what he was doing in northern Canada, he suddenly became aware that the frustration that had been dogging him had lifted. He realized that as he met the daily challenges of the outing, he had gained insights into his psychological quirks and learned how to work more effectively with his companions.

“These things teach you how to work as a team and get through hardships,” Massimino says in the book. “They teach you how to deal with adversity.” In space, as in extreme environments on Earth, self-awareness and adaptability can spell the difference between success and failure — and ultimately, between life and death.

Astronauts need intimate knowledge of their own strengths and limitations, as well as those of their crewmates. They must be able to put aside bad moods, personal feuds and mental distractions of any kind. That’s where the true survival element of NASA’s training kicks in.

Teamwork vs. intuition

Every astronaut has a version of that breakthrough moment from survival training.

For Linenger, it came when he realized he could have complete confidence in his guide and his fellow pilots during his jungle trip. For Leland Melvin, a former NFL player who flew on two space shuttle missions, it came during a NASA-sponsored course by the National Outdoors Leadership School in Canyonlands.

Melvin was hiking with an 85-pound backpack on when he came to a turbulent river, swollen by recent flash floods. As he began to ford the torrent, his feet got hopelessly stuck in the mud.

Astronaut Leland Melvin, STS-129 mission specialist, attired in a training version of his shuttle launch and entry suit, participates in a Full Fuselage Trainer (FFT) mock-up training session in the Space Vehicle Mock-up Facility at NASA's Johnson Space Center. James Blair / NASA

“I’m a wide receiver, so I’m doing ‘quick foot,'” he says. But the fast footwork exhausted him and only embedded his feet deeper in the muck. “Then I looked up,” he says, “and there’s this huge tree trunk coming down the river, about to impale me.”

With the tree bearing down, other members of his team called out to him from the bank, urging him to slow his movements. Melvin trusted their advice over his own instincts. He broke free from the mud just in time to save himself.

That ability to communicate quickly and effectively in the midst of any distraction — and to trust feedback even if it seems counterintuitive — helped Melvin on his later space missions. “There can be so much chatter going on,” he says of his time aboard the shuttle. “We may talk nonverbally just by tapping a checklist or by pointing to a bank of switches. It’s a deep connection, you just know what you’re going to do as team.”

Together as one

Massimino wound up spending more than eight hours on that spacewalk to fix Hubble. He might have given up had he not recognized his frustration — just like he had felt at Cold Lake.

“When I couldn’t get that handrail off, I started to get upset,” Massimino says. “I was ready to beat myself up about it. I wasn’t going to be able to do this job, and it was all my fault.” But this time, he knew how to manage his feelings. He willed himself to calm down and work the problem.

He also had help from his crewmate Drew Feustel, who had been through the same kind of training and who shared a deep bond as a result. Fuestel recognized his friend’s stress and talked him through it. “He knew the No. 1 thing for me was to try to keep my spirits up,” Massimino says.

As it turned out, the fix for Hubble’s balky bolt was using brute force — but just enough to do the job without damaging the $5-billion, bus-size space observatory or rip open his spacesuit.

Two days later, Massimino was back in his spacesuit, sizing up the rehabilitated Hubble like a proud dad as he and Feustel monitored its release from the shuttle Atlantis and sent it back into its regular orbit.

“Houston, Hubble has been released, it’s safely back on its journey of exploration as we begin steps to conclude ours,” astronaut Scott Altman, Massimino’s crewmate and the mission’s commander, reported back at the time. “Not everything went as we planned, but we planned a way to work around everything, with the whole team pulling together.”

If that sounds more like a motivational speaker than a space daredevil imbued with the Right Stuff, so be it. It’s precisely the kind of resolute, can-do camaraderie that astronauts like Massimino take from survival training.

“You learn how to be a leader, and also how to be a follower,” Massimino says. “You learn how to help each other be successful so that you get to be successful as a group.”

WANT MORE STORIES ABOUT ASTRONAUTS?

FOLLOW NBC NEWS MACH ON TWITTER, FACEBOOK, AND INSTAGRAM.