You talk about switching off, which might be worth expanding upon. Some people can more or less detach their thoughts from their physical discomfort, and some people can fly on instruments while very confused about their surroundings/situation.
The most demoralising thing in a race is thinking that you can’t finish it. That is actually a lot more likely to break people, than being hurt physically, or being shot to pieces mentally.
Being able to finish anything over 50 miles is 90% mental. It is honestly better to have a good mind-set, and cr4p athleticism, than vice versa. Everybody has quit in them somewhere, and the human brain is very cunning at making it acceptable to you.
When I undertake a challenging event, I tell everybody that I am going to do it, and freely post the online tracking link. It is a good incentive to know that your friends and family are watching you online.
I try to work out where the psychological low points of the race are going to be, and prepare myself for them. Examples are the half way mark, and indoor checkpoints.
If I undertake an event like say UTMB, I never book accommodation for the nights when I should be on the trail, or make any other preparations for failure. I literally put myself in a position, where the only way out is to finish the race.
My last psychological fall back, is “if you quit now, you are going to have to come back next year”. Not the most inspiring, but it seems to work.