There's something fishy that I don't get clearly with the action principle of classical mechanics, and the endpoints that need to be fixed (boundary conditions). Please, take note that I'm not interested in answers with the hamiltonian, which isn't the point here. I want to stay within the Lagrangian formalism. Suppose we have a general Lagrangian $L(q, \dot{q},t)$, with standard action \begin{equation}\tag{1} S[q] = \int_{t_1}^{t_2} L(q, \dot{q},t) \, dt. \end{equation} If I vary this action in the usual way with Dirichlet boundary conditions $\delta q^i(t_1) = \delta q^i(t_2) = 0$, then I get the usual Euler-Lagrange (EL) equations as is usually shown everywhere. This is all nice. But now I want to let the variations $\delta q^i$ to stay arbitrary at the boundary, but fix the canonical momentum $$ p_i(q,\dot{q},t)~:=~\frac{\partial L(q,\dot{q},t)}{\partial\dot{q}^i} $$ instead: $\delta p_i(t_1) = \delta p_i(t_2) = 0$. In this case, varying (1) can't give back the EL equations: \begin{align} \delta S &= \int_{t_1}^{t_2} \biggl( \frac{\partial L}{\partial q^i} \: \delta q^i - \frac{d}{dt} \Bigl( \frac{\partial L}{\partial \dot{q}^i} \Bigr) \delta q^i + \frac{d}{dt} \Bigl( \frac{\partial L}{\partial \dot{q}^i} \: \delta q^i \Bigr) \biggr) dt \\ &= \int_{t_1}^{t_2} \biggl( \frac{\partial L}{\partial q^i} - \frac{d }{d t} \Bigl( \frac{\partial L}{\partial \dot{q}^i} \Bigr) \biggr) \delta q^i \, dt + p_i \, \delta q^i \Bigr|_{t_1}^{t_2}. \tag{2} \end{align} Since $\delta q^i \ne 0$ at $t_1$ and $t_2$ (and in general $p_i \ne 0$ at the boundary), the last term doesn't cancel and we can't get the EL equations. If I really want to impose $\delta p_i(t_1) = \delta p_i(t_2) = 0$ (not $p_i(t_1) = p_i(t_2) = 0$ !), then I need to modify the Lagrangian. We're allowed to add a total time derivative: \begin{equation}\tag{3} \tilde{L} = L + \frac{dQ}{dt}, \end{equation} where $Q(q, \dot{q}, t)$ must be chosen wisely. In this case: \begin{equation}\tag{4} \tilde{S} = \int_{t_1}^{t_2} \tilde{L} \, dt = \int_{t_1}^{t_2} L \, dt + Q \Bigr|_{t_1}^{t_2}. \end{equation} But then, this is where I feel confused. If I use \begin{equation}\tag{5} Q(q, \dot{q}, t) = - q^i \, p_i, \end{equation} where $p_i = \partial L / \partial \dot{q}^i = p_i(q, \dot{q})$ is the canonical momentum from the "old" Lagrangian $L$, then the "new" lagrangian (3) have second order terms, i.e $\ddot{q}^i$ (from the total time derivative and $\dot{p}_i$). I don't think this really gives an issue since these second order terms are coming in a special way, i.e from a total time derivative. Varying the "new" action (4) give \begin{align} \delta\tilde{S} &= \int_{t_1}^{t_2} \biggl( \frac{\partial L}{\partial q^i} - \frac{d }{d t} \Bigl( \frac{\partial L}{\partial \dot{q}^i} \Bigr) \biggr) \delta q^i \, dt + p_i \, \delta q^i \Bigr|_{t_1}^{t_2} - (p_i \, \delta q^i + q^i \, \delta p_i ) \Bigr|_{t_1}^{t_2} \\ &= \int_{t_1}^{t_2} \biggl( \frac{\partial L}{\partial q^i} - \frac{d }{d t} \Bigl( \frac{\partial L}{\partial \dot{q}^i} \Bigr) \biggr) \delta q^i \, dt - q^i \, \delta p_i \Bigr|_{t_1}^{t_2}, \tag{6} \end{align} which is the desired result to use the boundary conditions $\delta p_i(t_1) = \delta p_i(t_2) = 0$ and get back the EL equations from the "old" Lagrangian.
So what is wrong with this? Why am I confused here? Is there something important that I'm missing? I tried to find something about this is Goldstein's book on Classical Mechanics, and also in Classical Dynamics by José-Saletan, but didn't found anything. I guess that if the procedure above was valid, then it would have been described in these books and elsewhere! So something must be wrong in the procedure above.
EDIT: I'm perplexed by the possibility that the imposed conditions may be incompatible with the Lagrangian or the equations of motions (EOM), without knowing the EOM in advance. For example, it's possible that the imposed positions $q_1^i$ at time $t_1$ and $q_2^i$ at time $t_2$ may be incompatible with the general solution to the EOM, so that we may need to use the momentum instead (or other conditions). Of course, the reverse is also true (incompatible momentum conditions, so need to use the coordinates instead). I don't remember that I saw this discussed before.
EDIT 2: The following arXiv paper confirms that the procedure shown above is right: https://arxiv.org/abs/0809.4033. In particular, from pages 15-17:
The bulk equations of motion are always unaffected by adding a total derivative, even one that contains higher derivatives of the fields. However, the addition of a total derivative may render the variational problem inconsistent, or require different boundary data in order to remain well-posed. ... As a general rule of thumb, if an action contains higher derivatives which appear only as total derivatives, a boundary term will need to be added.