Flooring was missing from a steam train toilet compartment for two months before a young boy nearly fell through the opening.
The three-year-old only avoided falling on to the track below because of the "quick reaction" of his mother who grabbed his arm, according to the Rail Accident Investigation Branch (RAIB).
They were both left shocked by the incident and the child suffered minor bruising, investigators said.
The accident occurred on the South Devon Railway (SDR) steam heritage line between Staverton and Buckfastleigh on June 22 last year as the train was travelling at around 20mph.
Engineers had dismantled the floor to access the carriage's braking system for repairs during the previous April.
Staff had placed a notice on the compartment door and attempted to secure it to prevent it being opened, but this was "inadequate", the RAIB said.
Over 100,000 people visit SDR each year.
The RAIB recommended that the railway commissions an independent review into the actions taken since the accident to address "deficiencies in its processes".
Chief Inspector of Rail Accidents Simon French said: "When a mother, enjoying a happy day out with a small child, is suddenly faced with a life-threatening hazard, it is important to find out what went wrong.
"In this case, the railway company took out the toilet floor to get at and fix a leaking brake pipe.
"The engineers found a lot of previously undetected rust and rot, that was going to have to be dealt with.
"It seemed like a good idea to seal up the toilet door until the whole carriage could be got into the workshops for overhaul, but that was not easy to do securely and the door came open.
"Our investigation found that the railway's safety management system was not operating properly and had not been reviewed for many years."