What happens when Wild Shape/Polymorph runs out in a space that’s too small?

So in a 5e campaign I’m running, the party Druid has recently been doing some scouting of the local monster caves in spider form to avoid notice. I’ve also described how the goblins and kobolds who live in these caves use a variety of tunnels too small for Medium sized creatures in order to get around. Since the party doesn’t have any halflings or gnomes, I didn’t really think the party would ever get into those tunnels. But obviously if a goblin can fit, so can a spider, so there is the possibility that the Druid might try to scout them out.

But what if something goes wrong? What if the Spiderdruid were to get lost and run out of time in Wild Shape, or encounter a hungry lizard and drop to 0 Spiderhealth?

Is there anything in the rules to suggest what should happen when somebody tries to revert to their normal size in a space where they normally wouldn’t fit?

With a space sized for a Small creature such as in my game, I might just rule that he’s stuck unless he makes difficult Dexterity checks or waits to get his Wild Shape back. However, what if it was an even smaller space, such as a burrow or a pipe or a deep crack in the rock? Someplace where his human body literally won’t fit?

If there aren’t any rules for this, I’m open to suggestions on how it can best be handled.