Backport of #15764 and #15902 to 4-0-stable

Backport of #15764 and #15902 to _4-0-stable_ branch. That was a Ruby breaking bug so It should be backported.