Could you have enter into this situation?
When Copters have an optical flow sensor enabled (along with a rangefinder) and it is specified as the only horizontal position source (e.g. EK3_SRCx_VELXY=OpticalFlow and EK3_SRCx_POSXY=None) and the vehicle is flying in a pilot controlled mode requiring a position estimate (ie Loiter or PosHold) the vehicle will not climb above the rangefinder’s maximum altitude specified in RNGFNDx_MAX_CM. This is a safety mechanism because otherwise the EKF failsafe would trigger as the vehicle flew out of rangefinder range.
You may want to consider disabling the rangefinder onboard the optical flow sensor and coupled it with an external longer range rangefinder to see it resolved.