Krav Maga training from many moons ago taught us that people who don't fight regularly, tend to focus solely on the attacking limb/weapon/etc and ignore other options that seem painfully obvious when you're in the 3rd person and don't have a massive shot of adrenaline racing through you. You need to take advantage of this weakness in an attacker, and remember not to be caught out yourself.
But if someone grabs your gun, you have a couple options that work pretty well. First is to remember that your legs are notably longer and stronger than your arms. While they have a grip on your gun, start kicking. You'll probably be limited to front kicks or stomps or low kicks aimed at knees, but kick hard and kick as fast as you can. Kick the shins, the knees, the balls, whatever. The second they let go to block your attack, you jump back and pull that trigger.
The other thing to remember is that your hips and back are much stronger than your arms. If kicking isn't working or isn't an option, redirect the momentum like a wrestler avoiding a hold. Instead of pulling backwards, push down and drive forward. Try to retract your arms and spin around in an attempt to pull them off their feet in a kind of throw. You might not throw them, but if they let go of the gun, you've achieved your goal.
Now, work on creating distance. Jump back. Start stomping ankles. If they dropped to a knee, leg kicks to the head are a great choice. You need to get away if you can. If you can't, at least they aren't holding your gun when you start pulling the trigger.