Integral with two different answers using real and complex analysis

The problem with the real approach is that you make the change of variable $t=\tan\left(\dfrac{\theta}{2}\right)$ for $0 < \theta < 2 \pi$.

This is problematic since your substitution need to be defined and continuous for all $\theta$, but you have a problem when $\theta=\pi$.

Edit: Note that if you split the integral into $\int_0^\pi+\int_\pi^{2 \pi}$, you are going to get the right answer, as for one integral you are going to get $\arctan(- \infty)$ and for the other $\arctan(+\infty)$:

$$\int_0^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}=\int_0^\pi \frac{\mathrm{d}θ}{2-\cos \theta}+\int_\pi ^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}\\ = \lim_{r \to \pi_-} \int_0^r \frac{\mathrm{d}θ}{2-\cos \theta}+ \lim_{w \to \pi_+} \int_w^{2 \pi} \frac{\mathrm{d}θ}{2-\cos \theta}\\= \lim_{r \to \pi_-} \left(\frac{2\tan^-1( \sqrt{3} \tan( \frac{ r}{2}))}{ \sqrt{3}}-0\right)+ \lim_{w \to \pi_+}\left(0- \frac{2\tan^-1( \sqrt{3} \tan( \frac{ r}{2}))}{ \sqrt{3}}\right).$$


Note that that tangent function, $\tan(x)$, is discontinuous when $x=\pi/2+n\pi$. So, the antiderivative $\frac2{\sqrt{3}} \arctan\left(\sqrt 3 \tan(\theta/2)\right)$ is not valid over the interval $[0,2\pi]$.

Instead, we have

$$\int_0^{2\pi}\frac{1}{2-\cos(\theta)}\,d\theta=2\int_0^\pi\frac{1}{2-\cos(\theta)}\,d\theta=\frac{4}{\sqrt3}\left.\left(\arctan\left(\sqrt 3 \tan(\theta/2)\right)\right)\right|_0^\pi=\frac{2\pi}{\sqrt3}$$