On iOS 15, modifying navigationItem adds redundant UIGestureRecognizers to Back button, which can result in app hangs and crashes

Number:rdar://FB9757126 Date Originated:2021-11-13
Status:Open Resolved:
Product:iOS - UIKit Product Version:15
Classification:Application Crash Reproducible:Yes
On iOS 15+, modifying a view controller's navigationItem will append redundant gesture recognizers to the Back button. When the number of gesture recognizers gets into the thousands, tapping on the Back button can lock up the main thread for many seconds, which can result in the application being killed by the watchdog timer.

Steps to reproduce:

1. Run the attached project on an iOS 15 device or simulator: https://github.com/bunnyhero/back-button-bug
2. Tap the "Go" button to advance to the test view controller.
3. Tap the "Change Title 100 Times" button and the "Change Right Bar Item 100 Times" button many times.
4. (Optional) Tap the "Print Gesture Recognizers to Console" to see the myriad duplicate gesture recognizers in Xcode debug out.
4. Tap the "Back" button

Expected results:
- Gesture recognizer count remaining stable. Back button responsive. (This is what happens if you run the project on iOS 14.)

Actual results:
- Gesture recognizer count climbs continuously on each change. If the total number is high enough (thousands), the Back button responds very slowly.

- Reproduced on iOS 15.1 as well as iOS 15.2 beta.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!