And if no other object receives all the work, then the outgoing gas acquires speed, which can be exploited in a turbine, or converts to heat, etc.
But applying to such cases thermodynamics, which is a theory of equilibrium, demands caution and precaution.
Many textbooks propose wrong exercises in this context.