Say an astronaut had some sort of psychotic break during a spacewalk, is there any mechanism or failsafe that would prevent the astronaut from opening their helmet and exposing themself to the vacuum of space during an EVA?
The closed helmet is under pressure of the air inside the suit if the outside is the vacuum of space. The helmet is opened easily when both pressures are equal. But when the inside pressure is higher, a lot of force would be needed to open the helmet.
So the helmet may be opened easily only when worn inside a pressurized chamber but not in a vacuum.
All of the past NASA spacesuit helmets cannot be removed under pressure. However, many spacesuits have had visors (faceplates) which can be opened. Unless noted, the source below is Dressing for Altitude: U.S. Aviation Pressure Suits -— Wiley Post to Space Shuttle (530 pages, 18 MB).
As described below, each NASA spacesuit has had a detachable helmet, which attaches to a rigid neckring on the suit body. Several different "positive locking mechanisms" have been used -- bayonet mounts, locking balls, and latch dogs -- but in each case, the pressure difference between inside and outside the suit creates a locking force. It's exactly the same reason why you can't open a pressure cooker when it is under pressure. The first such helmet attachment was invented by the David Clark Company in 1948:
A second suit, Experiment B, was constructed on November 22, 1948, using a bladder-and-case construction technique similar to a G-suit. [...] An unusual plexiglass helmet was attached using a bayonet seal at the neck, and a compressed-air fitting was installed in the rear left waist area.
The first aviation pressure suit was constructed by the B.F Goodrich company in 1934 for pilot Wiley Post. Having previously been the first pilot to fly around the world, Wiley realized that the higher he flew, the faster he could fly. With the Goodrich pressure suit, Wiley became the first person to fly in the stratosphere, where he discovered the jet stream.
The U.S. Navy contracted Goodrich to produce a series of flight suits for Navy pilots. The Mark II version introduced a faceplate which could be pivoted upwards to open the face of the helmet:
The helmet, in particular, was innovative and served as the inspiration for several David Clark Company helmets that followed. The retractable clear visor used a pneumatic seal around its perimeter that inflated automatically as it was lowered. The breathing regulator was located on the left side of the helmet and included an “ON-OFF” oxygen control and a visor-seal deflation button. An adjustment knob for sizing the internal straps and pads that held the face seal against the face was located on the right side of the helmet. The clear and tinted visors retracted upward into an enclosed space intended to provide protection against scratching.
The seals on the Mark III helmet were the same:
The Mark III helmet had both clear and tinted visors mounted on top, rather than under a protective cover as on the Mark II, allowing the helmet to be somewhat lighter weight. An inflatable pressure seal around the helmet opening acted upon the clear visor.
as were those on the Mark IV:
The Mark IV helmet was generally similar to the Mark III unit. The helmet was constructed from phenolic resins and fiberglass cloth and was designed to offer maximum visibility, comfort, mobility, windblast protection, crash protection, and simplicity of operation. It had an internal suspension system and a built-in AIC-10 communication system. The retractable visor was made of Plex II Plexiglas and used a pneumatic seal when closed.
The Navy Mark IV was the basis for the Mercury spacesuit. If they wanted to open their helmet, they could just open the visor, rather than try to take it off.
Later in the 1960s, the Navy decided to switch from the full-pressure suits made by Goodrich to partial-pressure suits. This ended Goodrich's role as a flight suit manufacturer.
The newly-formed U.S. Air Force chose the David Clark Company as their flight suit contractor. As noted above, David Clark invented the bayonet helmet seal in 1948, and copied Goodrich's idea of a flip-up visor. Several generations of Air Force suits replaced the bayonet ring was replaced with locking balls, and then latch dogs:
An improved neck ring was similar to the one developed for the Gemini suit. On the A/P22S-2, it was necessary to position the helmet, seat it in the ring around the entire circumference, hold it in place, and then secure its locking ring. The improved neck ring replaced the locking balls with stainless steel latch dogs that held the helmet securing in place while the pilot rotated the locking ring.
NASA chose David Clark to produce the Gemini spacesuits. They borrowed heavily from their Air Force suits. However, instead of sealing the visor with an inflatable seal (like the Mercury suits), the held the visor closed with a mechanical latch:
The S901J helmet was less complicated than the A/P22S-2 helmet. For instance, the previous pneumatic seal around the visor was replaced with a Gemini-style static seal that was activated mechanically via a visor-closing bailer bar.
So like the Mercury suits, you can't remove the helmet, but you can open the visor under pressure.
The Apollo program used a single spacesuit, made by ILC Dover (part of International Latex). It used a fishbowl-style "bubble helmet" with a positive locking mechanism:
[...] replacement of the neckring with a more reliable and positive locking type that had retractable latches instead of the single-piston-ring type used in the A5L design
What Apollo calls a "visor" is nothing like the visors of the other suits mentioned here; the face of the Apollo helmet does not open.
So no taking off or opening this helmet.
Two categories of suits were used on the Shuttle. The Extravehicular Mobility Unit was used for spacewalks, and was built by ILC Dover. Like their previous Apollo suit, the helmet is attached with a positive locking mechanism, and the face of the helmet does not open. So no suicide with this suit.
Several models of suits were made for use inside the cabin, all by David Clark. In the years since Gemini, they continued to make suits for the Air Force. In particular, the S103x series suits were derived from the Gemini spacesuit, to be used in the SR-71 Blackbird. In turn, various Shuttle launch suits derived from that series:
The S1030A was the Ejection Escape Suit, used on STS-1 to 4.
The S1032 was the Launch Escape Suit, used on STS-26R through STS-88.
The neck ring had a latch that secured the helmet to the suit. Sliding the latch halves together moved six latch-dogs to secure the helmet on the neck ring. Sliding them apart retracted the dogs, allowing the helmet to be removed from the neck ring. Two independently operating polycarbonate and acrylic visors provided a clear pressure visor and a dark sunshield. The crewmember closed and locked the pressure visor by pulling the visor and the bailer bar down into the locked position. To open the pressure visor, a latch on the bailer bar lock had to be pushed down and two buttons on either side of the lock pressed. This allowed the bailer bar to unlock, after which the visor could be opened.
The S1035 was the Advanced Crew Escape Suit, used on STS-64 and later.
Like the previous Gemini suits, when pressurized the helmet could not be removed, but the visor could be opened.
In summary, no helmet could be removed under pressure. In particular, your scenario won't work with an extravehicular (spacewalk) suit.
However, many intravehicular suits had visors that could open. Then again, they normally operate within the pressurized cabin, so your scenario is unlikely.
Some of these helmets are difficult to take off (or open the visor) with gloves on.
It's also worth mentioning that prospective astronauts get a lot of training. Any person who would panic or hesitate to follow procedures would likely wash out of the program.