Are you supposed to use the internal or external pressure for the $pV$ work integral? To introduce the $pV$ work integral, my physics textbook gives me the classic setup of a vertical piston (which has face area $A$) with a gas of pressure $p$ and initial volume $V_i$ (please see the image for the exact setup). On the piston is lead shot which exerts a downward force. The force exerted by the gas is given by $F=PA$. Therefore, the work done by the gas is given by the integral from the initial to final volume of $Fdx=pAdx=pdV$ (please see the image for the full derivation; only the full paragraph is relevant).

This derivation makes sense. However, I have seen formulations of the integral in terms of external pressure rather than internal pressure. Namely, I have seen the differential work written as $dW=-p_{ext}\cdot dV$. I understand that the negative sign comes from the chemistry sign convention, but am confused whether to use the internal or external pressure.
In two different cases, these two integrals seem to produce different results.
Case 1 - Adiabatic Expansion: My book suggests that an adiabatic expansion can be achieved by the following setup: a completely thermally insulated gas piston with lead shot (see image).

As one shot is removed at a time from the piston, the gas expands adiabatically. In this case, the external pressure is constant and the internal pressure varies with volume. I would expect the two integrals (d$W=p\cdot dV$ and $dW=p_{ext}\cdot dV$) to be different, but am not completely sure if this is true.
Case 2 - Free Expansion: In the free-expansion setup, a gas is allowed to expand into a vacuum chamber (see image).

The gas has pressure p, but the external pressure p_ext is zero. Therefore, the first integral, which uses $dW=pdV$ has a non-zero value while the second integral $dW=p_{ext}\cdot dV$ is equal to zero. The second integral produces the correct result while the first does not.
Again, which pressure is correct to use for these integrals.
This is my first post so I apologize for any formatting errors that occurred.
Thank you so much!
Please note that throughout this question, I have used $p$ to refer to internal pressure of the gas and $p_{ext}$ to refer to external pressure.
 A: For your case 1:
I think your book refers to Quasistatic process.
It is a form of idealized process which is "infinitely slow" so the system is always in
thermodynamic equilibrium with the surroundings.
What this means in this context is that $p_{ext} =p$.
So it really does not matter which one you use magnitude wise.
The correct formula for work done by the gas is $$W=\int_{V_{initial}}^{V_{final}}p_{ext}\cdot dV$$
Regarding:

In this case, the external pressure is constant and the internal pressure varies with volume.

Note both internal and external(due to change in weight) pressures are changing extremely slowly and continuously. So you have the integrate over whole process.
For case 2:
Work done by gas in free expansion is indeed $0$ as $p_{ext}=0$. So there is no external force acting on the gas , hence no work is done on the gas. Also there is nothing the gas can exert force on so work done by the gas is also $0$.
Also note that this process is "non-reversible" or "non Quasistatic" so it makes no sense to use the formula derived using the assumption of "reversible" process. Even though it gives the correct answer.
A: The ideal gas law describes the relationship between pressure, volume, and temperature of an ideal gas at thermodynamic equilibrium.  It also describes the PVT relationship for a gas experiencing a very slow deformation process (quasi-static, reversible), since a reversible process is just a continuous sequence of closely neighboring thermodynamic equilibrium states.  But for a rapid deformation of a gas, the ideal gas law no longer allows you to calculate the gas pressure correctly (especially at the moving boundary where the gas is doing work), because it applies only at thermodynamic equilibrium, and a rapid irreversible process passes through a sequence of non-equilibrium states.  We know from fluid dynamics that, what is happening in an irreversible rapid-deformation process is that "viscous stresses" contribute to the pressure at the moving boundary.  So the pressure must differ from the ideal gas law.
Now for $P_{ext}$ vs P: $P_{ext}$ is supposed to represent the pressure of the surroundings at the moving boundary where work is being done, and P is supposed to represent the pressure of the gas at this interface.  Whether a process is reversible or irreversible, by Newton's law of action-reaction, we must always have that $P=P_{ext}$.  And, for thermodynamic equilibrium or for a reversible process, P can be determined from the ideal gas law (or other real-gas equation of state).  But, for an irreversible process, we can't use the ideal gas law, so we are more limited.  To calculate the work done at the moving boundary, we must impose the external pressure manually or by an automatic control system to dictate the pressure for calculating the work done by the gas on its surroundings.
So, in summary, for all processes, both reversible or irreversible, the work done on the surroundings is $$W=\int{P_{ext}dV}=\int{PdV}$$However, for an irreversible process, we can not calculate P from the ideal gas law, so we are stuck using $P_{ext}$, which must be specified by other means.
