Gallows humorGallows humor is "humor that treats serious, frightening, or painful subject matter in a light or satirical way." It has the connotation of someone laughing at their own execution.
Urban Dictionary actually has a good summary:
gallows humor
Sense of humor that arises from stressful, traumatic or life-threatening situations where death seems to be an inevitability. Different from black humor in that comments of this sort are made by the person/persons affected (i.e. about to die).
It is not always used to mean that the person is facing literal death, but the idea is the same. They are expressing amusement or using humor in the face of their own inevitable fate.