It's definitely a thing you don't do without good reason (enemy boarding action/mutiny) but remember subs are designed to survive nearby explosions underwater; there's redundancy and there's massive amounts of time spent training damage control where your repair or work around damaged systems.
A 9mm, buck shot, or even a 556 (which they are not using on a sub) is not piercing the pressure hull of a submarine. That's before considering all the other mass of metal and matter within the sub.
I mean, I guess there's more risk of ricochets and equipment damage on a sub than a ship? Regardless, a lot of "don't do that" things become "you might have to do that" things in a combat scenario.
I grew up watching Voyage to the Bottom of the Sea... those guys were always needing to break out the small arms. Apparently lots of rowdy sea critters looking to get in the boat.
BBC made a fascinating documentary called Perisher on the qualifying process for sub captains in the Royal Navy - it's available on YouTube (in the US at least)
https://www.youtube.com/watch?v=I1LF2I3fTbY