You are substituting $u=\tan(x)$ on the interval $x\in[0,\pi]$. Notice what $\tan(x)$ does in that interval. It has a discontinuity at $\pi/2.$
Thus it seems prudent to notice that the integrand is even about $\pi/2$ and take $$ \int_0^\pi\frac{dx}{\sin^2(x)+1} = 2\int_0^{\pi/2}\frac{dx}{\sin^2(x)+1}$$
The antiderivative you'd get at the end if you powered through that last integral is $$ \frac{1}{\sqrt{2}}\arctan(\sqrt{2}\tan(x))+C$$ so you'd get $$ \frac{2}{\sqrt{2}}(\arctan(\infty)-\arctan(0)) = \frac{2}{\sqrt{2}}\frac{\pi}{2} = \frac{\pi}{\sqrt{2}}.$$
Note we could also tell something was fishy by the fact that our antiderivative at the end is not continuous at $\pi/2.$ The antiderivative of an integrable function is always continuous.
So... what is the antiderivative on $[0,\pi]$ (or on the whole real line for that matter)? (Or, I should really say, the function we subtract $F(b)-F(a)$ to get the definite integral)
We can write it (or a version of it, up to an arbitrary constant) as $$ F(x) = \int_0^x \frac{dt}{\sin^2(t)+1}.$$ When the formal antiderivative jumps down by $\pi/\sqrt{2}$ at $x=\pi/2,$ we need to compensate by adding $\pi/\sqrt{2}$ to make the function continuous. This will happen again at $3\pi/2.$ So the antiderivative will be $$ \int_0^x \frac{dt}{\sin^2(t)+1} = \frac{1}{\sqrt{2}}\arctan(\sqrt{2}\tan(x)) + C(x)$$
where $C(x)$ is a staircase function that is locally constant, and zero on the interval $(-\pi/2,\pi/2)$ but jumps up by $\pi/\sqrt{2}$ each time we hit an odd multiple of $\pi/2.$
Which brings us back around to your problem where the bounds were the same, so it looked like you should get zero. That was from $\tan(\pi) = \tan(0)$ which corresponds to the formal antiderivative also being the same at the endpoints. But we now know that we need to add in an extra $\pi/\sqrt{2}$ since $C(\pi) - C(0) = \pi/\sqrt{2}.$