AVAudioEngine crash when connecting inputNode to mainMixerNode

I have the following code to connect inputNode to mainMixerNode of AVAudioEngine:

   public func setupAudioEngine() {
       self.engine = AVAudioEngine()
       let format = engine.inputNode.inputFormat(forBus: 0)
      //main mixer node is connected to output node by default
       engine.connect(self.engine.inputNode, to: self.engine.mainMixerNode, format: format)
       do {
          try self.engine.start()
       catch {
          print("error couldn't start engine")
       engineRunning = true

But I am seeing a crash in Crashlytics dashboard (which I can't reproduce).

  Fatal Exception: com.apple.coreaudio.avfaudio
 required condition is false: IsFormatSampleRateAndChannelCountValid(format)

Before calling the function setupAudioEngine I make sure the AVAudioSession category is not playback where mic is not available. The function is called where audio route change notification is handled and I check this condition specifically. Can someone tell me what I am doing wrong?

Fatal Exception: com.apple.coreaudio.avfaudio
   0  CoreFoundation                 0x99288 __exceptionPreprocess
   1  libobjc.A.dylib                0x16744 objc_exception_throw
   2  CoreFoundation                 0x17048c -[NSException initWithCoder:]
   3  AVFAudio                       0x9f64 AVAE_RaiseException(NSString*, ...)
   4  AVFAudio                       0x55738 AVAudioEngineGraph::_Connect(AVAudioNodeImplBase*, AVAudioNodeImplBase*, unsigned int, unsigned int, AVAudioFormat*)
   5  AVFAudio                       0x5cce0 AVAudioEngineGraph::Connect(AVAudioNode*, AVAudioNode*, unsigned long, unsigned long, AVAudioFormat*)
   6  AVFAudio                       0xdf1a8 AVAudioEngineImpl::Connect(AVAudioNode*, AVAudioNode*, unsigned long, unsigned long, AVAudioFormat*)
   7  AVFAudio                       0xe0fc8 -[AVAudioEngine connect:to:format:]
   8  MyApp                       0xa6af8 setupAudioEngine + 701 (MicrophoneOutput.swift:701)
    9  MyApp                       0xa46f0 handleRouteChange + 378 (MicrophoneOutput.swift:378)
    10 MyApp                       0xa4f50 @objc MicrophoneOutput.handleRouteChange(note:)
  11 CoreFoundation                 0x2a834 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__
  12 CoreFoundation                 0xc6fd4 ___CFXRegistrationPost_block_invoke
  13 CoreFoundation                 0x9a1d0 _CFXRegistrationPost
  14 CoreFoundation                 0x408ac _CFXNotificationPost
  15 Foundation                     0x1b754 -[NSNotificationCenter postNotificationName:object:userInfo:]
  16 AudioSession                   0x56f0 (anonymous namespace)::HandleRouteChange(AVAudioSession*, NSDictionary*)
  17 AudioSession                   0x5cbc invocation function for block in avfaudio::AVAudioSessionPropertyListener(void*, unsigned int, unsigned int, void const*)
  18 libdispatch.dylib              0x1e6c _dispatch_call_block_and_release
  19 libdispatch.dylib              0x3a30 _dispatch_client_callout
  20 libdispatch.dylib              0x11f48 _dispatch_main_queue_drain
  21 libdispatch.dylib              0x11b98 _dispatch_main_queue_callback_4CF
  22 CoreFoundation                 0x51800 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
  23 CoreFoundation                 0xb704 __CFRunLoopRun
  24 CoreFoundation                 0x1ebc8 CFRunLoopRunSpecific
  25 GraphicsServices               0x1374 GSEventRunModal 
  26 UIKitCore                      0x514648 -[UIApplication _run]
  27 UIKitCore                      0x295d90 UIApplicationMain
  28 libswiftUIKit.dylib            0x30ecc UIApplicationMain(_:_:_:_:)
  29 MyApp                       0xc358 main (WhiteBalanceUI.swift)
 30 ???                            0x104b1dce4 (Missing) 


Did you find a solution for this?