The key bit is that you have the Dx = netD(x) evaluation as a grad-requiring bit that would be backwarded through. Note how the the example you cite uses output = netD(fake.detach()).view(-1) before the second backward. It detaches fake, so the backward doesn’t go through the netG computation and then computes the netD evaluation which has not been used in the first backward (which has its own netD evaluation).
If you don’t have much computation between the two backwards (if l2loss is what it sounds like) and want to accumulate grads for both losses in the netD parameters, maybe doing the backward through the sum of the two losses is a good option. (It should be more efficient than using retain_graph=True in the first backward, although that would work, too.)
Best regards
Thomas