Contour integration for functions with residues that form an infinite oscillating sequence I would like to evaluate some complicated integrals involving the hyperbolic secant, but the extension of the usual contour integration evaluation using the residue theorem isn't clear to me. I've been considering a simple example with a known solution $$\int_{-\infty}^{\infty} \text{Sech}\Big(\frac{\pi s}{2}\Big) \ d s \ = \ 2$$
This question is related as it discusses functions with an infinite number of poles, but there it is assumed that the sum of the residues converges. As is explained in that question, we are only ever actually considering bounded contours and then taking the limit that they grow arbitrarily large but I am not sure how that justifies my approach in this problem.
Suppose I am quite cavalier and I attempt to evaluate this integral by closing the contour along a semi-circle in the upper-half complex plane. Without really checking, I claim that the integral along the circular part of the contour vanishes and thus the integral over the real line is given by $$\sum_{n=0}^{\infty} 2 \pi i \ \text{Res}\Big( \text{Sech}\Big(i\pi\big (n+\frac{1}{2}\big)\Big) = 4-4+4-4 \ . . .$$ which clearly does not converge. However, if we consider the average of the first $m$ partial sums $$\frac{1}{m}(4+0+4+0+ \ . . . ) = \left \{ \begin{array}{lr} 2 & \  m \in  \text{evens}\\ 2 + \frac{4}{m}& m \in \text{odds} \end{array} \right.$$ which goes to $2$ as $m \to \infty$. This procedure seems totally ad hoc, even magical, but it predicts the correct answer. The first question is, why does the limit of the average of the partial sums give the correct answer?
Another way of regularizing this problem is to instead compute $$\lim_{\eta \to 0}\int_{-\infty}^{\infty} \text{Sech}\Big(\frac{\pi s}{2}\Big) e^{i  \eta \ s} \ d s $$ and then using the same procedure $$\lim_{\eta \to 0}\sum_{n=0}^{\infty} 2 \pi i \ \text{Res}\Big( \text{Sech}\Big(i\pi\big (n+\frac{1}{2}\big) e^{-\eta (2n-1)}\Big) =\lim_{\eta \to 0} \frac{4e^{\eta}}{1+e^{2\eta}} = 2$$ also gives the correct answer. So, there seems to be something to this method.
If I am more careful about the circular piece of the contour, I can show that its contribution is exponentially small almost everywhere. $\text{Sech}(x + iy)$ clearly decreases exponentially when $x$ is large, but what about when $y$ is large and $x$ is small? By rewriting $x + iy = re^{i \theta}$ and taking the absolute value, I can show that when $\theta = \frac{\pi}{2} +\epsilon, \ \left|\epsilon\right|<1$ (ie we are close to the imaginary axis), then $$\left| \text{Sech}(re^{i\theta})\right|\sim e^{-\frac{1}{2}\pi r \left|\epsilon\right| } : \ \ r\to \infty$$ My understanding of the Riemann integral suggests that the single point where $\epsilon = 0$ does not change the value of the integral along the contour.
So, it seems like I do have good reason to argue that the contribution from the circular contour vanishes. Is there a way, then, to justify my regularized sum of the residues as the correct answer in general or is it just an accident for this special case?

There is a much easier way to use contour integration to evaluate the integral: use a different contour.  I recommend a rectangular contour $C$, with vertices: $\{-R,R,R+2 i,-R+2 i\}$.  That way, we have only one pole $z=i$ inside $C$. Thus:
$$\oint_C \frac{dz}{\cosh{\frac{\pi}{2} z}} = \int_{-R}^R \frac{dx}{\cosh{\frac{\pi}{2} x}} + i \int_0^2 \frac{dy}{\cosh{\frac{\pi}{2} (R+i y)}} \\+\int_{R}^{-R} \frac{dx}{\cosh{\frac{\pi}{2} (x+2 i)}} + i \int_2^0 \frac{dy}{\cosh{\frac{\pi}{2} (-R+i y)}} $$
The second and fourth integrals vanish as $R \to \infty$.  The first and third integrals are equal, as $\cosh{\frac{\pi}{2} (x+2 i)} = -\cosh{\frac{\pi}{2} x}$, and we reverse the direction of the third integral.
On the other hand, the contour integral is $i 2 \pi$ times the residue at the pole $z=i$, which is $-i 2/\pi$.  (I'll leave that to the reader.)  Then we have
$$2 \int_{-\infty}^{\infty} \frac{dx}{\cosh{\frac{\pi}{2} x}} = i 2 \pi \left ( -i \frac{2}{\pi}\right ) = 4$$
The sought-after result follows.
