Close read pipe immediately after reading and only when initialized

c.f. #22386.