This summer, the story of a Thai soccer team trapped in a cave ended happily, when all 12 players and their coach were rescued by divers. But Elon Musk was less than thrilled. Spurned by the Thai government when he attempted to save the day with his own “kid-size submarine” and crew, he baselessly accused one of the rescuers—Vernon Unsworth, a British cave diver who was openly critical of Musk’s efforts—of being a pedophile. In the following weeks, Musk apologized, but ultimately doubled down on his arrogant, baseless accusation, at various points reigniting a controversy that shouldn’t have existed to begin with. “You don’t think it’s strange he hasn’t sued me?” he said on Twitter in August referring to Unsworth. “He was offered free legal services.” Weeks later, Unsworth filed a defamation lawsuit against Musk. (Musk did not comment on the defamation suit.)

It would hardly be the last time Musk’s ego came back to bite him. On Thursday, the Tesla C.E.O. was sued by the S.E.C. for allegedly making “false and misleading” statements when he announced via Twitter in August that he’d secured funding to take Tesla private. (Although Musk did meet with representatives for Saudi Arabia’s sovereign wealth fund, the group opted against backing Tesla’s take-private plan). The fraud lawsuit, Musk said on Thursday, is “unjustified.” Tesla’s board, too, is standing by Musk. If found guilty, Musk could be banned from serving as C.E.O. or director of the board of any public company.

But according to CNBC’s Andrew Ross Sorkin, the lawsuit could have been avoided entirely. On Friday morning, Sorkin reported that Musk had been on the verge of a settlement with the S.E.C. that wouldn’t have put his job on the line. Under the terms of the settlement, Musk and Tesla would reportedly have had to pay a nominal fine, but Musk wouldn’t have had to admit any wrongdoing. However, the agreement would have barred Musk from being chairman of his company for two years, and Tesla would have to appoint new independent directors. Sources familiar with the matter told CNBC that Musk decided to pull out of the deal at the last minute because he felt that “by settling he would not be truthful to himself.” The S.E.C. declined to comment, and Tesla was not immediately available for comment.

Update: On Saturday, the Securities and Exchange Commission announced that Musk had settled the lawsuit. Under the terms of the agreement, Musk will be allowed to remain C.E.O. but must resign as chairman of Tesla’s board and pay a $20 million fine. Tesla will also pay another $20 million fine to the S.E.C., and must appoint two new independent directors to its board. Musk would be able to return to his chairman position after a three year ban.

Musk’s pride, of course, is also the very thing that propelled Tesla to its current lofty position in the tech world. His vision is intrinsically tied to the company’s success—so much so that The Wall Street Journal fretted on Friday that “If Mr. Musk were forced to step down . . . Tesla would be left without the visionary entrepreneur who has captivated investors with his grand ambition to replace the internal combustion engine with electric, self-driving systems.” (Shares of Tesla dropped more than 10 percent in early trading Friday morning.) There’s no doubt that Musk has made society-changing contributions, revolutionizing both space travel and environmentally friendly vehicles. But the way Musk responds when he’s challenged shows how dangerous his ego can be. When Tesla drivers have died while using Tesla’s autopilot function, Musk has found it within himself to blame both the media and the victim. He has blown up at analysts who asked fairly routine questions during an earnings calls.

The S.E.C.’s investigation into Musk was never a particularly good look for a man who’s frequently raged against short-sellers. But by refusing to comply with the agency, he’s now essentially put his own job—and any other job at the head of a publicly held company—on the line.

This article has been updated.