Fixes #17621.  Backport 560b804f-0ae3-0310-86f3-f6aa0a117693 to 1.8.