The $\arctan$ function is multi-valued. When it appears in formulae for indefinite integrals you have to be careful when using it to calculate definite integrals, since in practice you use the principal branch ${\rm Arctan} : {\Bbb R} \cup {\infty}\to (-\pi/2,\pi/2]$ to evaluate the expression.
Let me illustrate by a simplified example:
$$ x = \int dx = 2\arctan(\tan(x/2)).$$
Again, calculating $\int_0^{2\pi} dx = 2\pi$ leads to the same paradoxe as yours if you evaluate using ${\rm Arctan}$ on the RHS. To solve the paradoxe note that there is a constant of integration which was omitted in the formula. You may change this constant when $\tan$ goes through a singularity $x=(2k+1)\pi$, $k\in {\Bbb Z}$ so as to make the result a continuous function.
Thus for my integral, if I want a continuous indefinite integral I should split ${\Bbb R}$ into intervals between these singularities:
$$ x =\int dx = 2{\rm Arctan} (\tan(x/2)) + 2\pi k, \ \ \ \ \ \ {\rm when } \ \ x\in \left((2k-1) \pi, (2k+1) \pi\right], k\in {\Bbb Z}.$$
which now is continuous and an identity for all $x\in {\Bbb R}$.
A drawback is, of course, that the formula becomes somewhat indigestible to read.
\displaystyle\lim_{x \to \pi^-}\left(2\arctan\left[1-2\tan\frac{x}{2}\right]\right)=\frac{-\pi}{2}$ and $\displaystyle\lim_{x \to \pi^+}\left(2\arctan\left[1-2\tan\frac{x}{2}\right]\right)=\frac{\pi}{2}.$ Is what I did above correct? – Asher2211 Jun 18 '21 at 03:37$$-\frac{1}{\sqrt{3}}\left(\lim_{x\rightarrow\pi-}2\arctan\big(1-2\tan(x/2)\big) - \lim_{x\rightarrow-\pi+}2\arctan\big(1-2\tan(x/2)\big)\right)\ =-\frac{1}{\sqrt{3}}\big(2(-\pi/2)-2(\pi/2)\big)=\frac{2\pi}{\sqrt{3}}$$ This is in accordance with my numerical computations. – Mittens Jun 18 '21 at 04:05