Backport #4224 to 3-2-stable.

This is a version of #4481 that backports the functional changes of #4224 while attempting to be minimally invasive.