On the other hand, Onan, who failed to impregnate his brother's widow in order to give his brother heirs, was struck down.
Even this is misinterpreted in modern religious circles to mean that masturbation is bad, that's not what's happening here.
As you said, it was about not giving his brother heirs, his 'sin' wasn't spilling the seed, it was the context around that spilling of seed.
His 'sin' was greed (because if his brother had no heirs, he stood to inherit his brother's possesions).
I mean, I'm of the opinion that it's all bullshit anyways, but it's interesting to see how those who do 'belive' have NO IDEA what the book that they literally believe was inspired (or occasionally literally written) by the creator of the universe actually says. If I thought that a book contained the literal secrets of the universe, I'd at least read it cover-to-cover once!