Bug: Crash during initial folder sync when adding subfolders to an EWS account with unified folders
Status: RESOLVED
Priority: P1
Resolution: FIXED
Created: 2024-04-04 14:21:37+00:00
Resolved: 2024-07-15 11:47:57+00:00

When I was testing account setup with EWS, wasn't able to fully sync the folder hierarchy due to a recurring crash. For reproducibility, I was using the test O365 account with https://phabricator.services.mozilla.com/D205972 checked out, and the folder that was causing it was `Inbox/kaie`.

Relevant log lines right before crashing:

```
[Parent 1361266, Main Thread] WARNING: NS_ENSURE_SUCCESS(rv, rv) failed with result 0x80550013: file /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/protocols/ews/EwsIncomingServer.cpp:134
error while syncing: [1361266] Assertion failure: isSecurityError, at /home/babolivier/Documents/mozilla/mozilla-central/xpcom/base/ErrorNames.cpp:32
```

* The second line is printed out by https://hg.mozilla.org/comm-central/file/02e053a8f57be4de6180effddabaf7f0dea2bddc/rust/ews_xpcom/src/client.rs#l50 (the file isn't on searchfox yet at the time of filing this bug).
* The failing assertion is https://searchfox.org/mozilla-central/rev/8340afd1fb3e7b2727d2efb47df1bd3112db7213/xpcom/base/ErrorNames.cpp#32, because the error is `NS_MSG_FOLDER_EXISTS` (`0x80550013`, same as mentioned in the first line), which is neither an error defined in m-c nor coming from `NS_ERROR_MODULE_SECURITY`, since it's an error code used only in Thunderbird.
* This error initially originates from https://searchfox.org/comm-central/rev/af8654ccb3642337eba46dcbea98b76fcb05354f/mailnews/base/src/nsMsgDBFolder.cpp#3360 (before making its way to `EwsIncomingServer.cpp`).

Upon digging and debugging, it looks like creating this folder fails because we try to create this folder twice. The first time with this stack (breakpoint at the start of `nsMsgDBFolder::AddSubfolder`):

```
#0  nsMsgDBFolder::AddSubfolder (this=0x7fffb1b1a5c0, name=..., child=0x7fffffffb468) at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/base/src/nsMsgDBFolder.cpp:3309
#1  0x00007fffec49b60f in nsMsgBrkMBoxStore::CreateFolder (this=<optimized out>, aParent=0x7fffb1b1a5d0, aFolderName=..., aResult=0x7fffffffb568)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/local/src/nsMsgBrkMBoxStore.cpp:270
#2  0x00007fffec52a4e0 in EwsIncomingServer::CreateFolderWithDetails (this=0x7fffacc4e4c0, id=..., parentId=<optimized out>, name=..., flags=4)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/protocols/ews/EwsIncomingServer.cpp:133
#3  0x00007fffedbedb1c in xpcom::interfaces::idl::IEwsFolderCallbacks::Create (self=0x7fffb1b1a5c0, id=0x7fffffffb7f0, parentId=0x7fffffffb7d0, name=0x7fffffffb9e0, flags=32)
    at /home/babolivier/Documents/mozilla/mozilla-central/obj-x86_64-pc-linux-gnu/dist/xpcrs/rt/IEwsClient.rs:159
#4  ews_xpcom::client::{impl#0}::push_sync_state_to_ui::{async_fn#0} () at ews_xpcom/src/client.rs:266
#5  ews_xpcom::client::{impl#0}::sync_folder_hierarchy_inner::{async_fn#0} () at ews_xpcom/src/client.rs:128
#6  0x00007fffedbe9c79 in ews_xpcom::client::{impl#0}::sync_folder_hierarchy::{async_fn#0} () at ews_xpcom/src/client.rs:47
```

And the second one with this stack:

```
#0  nsMsgDBFolder::AddSubfolder (this=0x7fffb1b1a5c0, name=..., child=0x7fffffffb0b0) at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/base/src/nsMsgDBFolder.cpp:3309
#1  0x00007fffec49ab81 in nsMsgBrkMBoxStore::AddSubFolders (this=this@entry=0x7fffbd8d9a80, parent=parent@entry=0x7fffb1b1a5d0, path=..., deep=true)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/local/src/nsMsgBrkMBoxStore.cpp:1149
#2  0x00007fffec49a1ac in nsMsgBrkMBoxStore::DiscoverSubFolders (this=0x7fffbd8d9a80, aParentFolder=0x7fffb1b1a5d0, aDeep=true) at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/local/src/nsMsgBrkMBoxStore.cpp:235
#3  0x00007fffec529c74 in EwsFolder::GetSubFolders (this=0x7fffb1b1a5c0, aSubFolders=...) at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/protocols/ews/EwsFolder.cpp:133
#4  0x00007fffec19fda2 in nsMsgDBFolder::GetChildWithURI (this=0x7fffb1b1a5c0, uri=..., deep=false, caseInsensitive=true, child=0x7fffffffb104)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/base/src/nsMsgDBFolder.cpp:3149
#5  0x00007fffec1a1538 in nsMsgDBFolder::AddSubfolder (this=0x7fffb1b1a5c0, name=..., child=0x7fffffffb468) at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/base/src/nsMsgDBFolder.cpp:3362
#6  0x00007fffec49b60f in nsMsgBrkMBoxStore::CreateFolder (this=<optimized out>, aParent=0x7fffb1b1a5d0, aFolderName=..., aResult=0x7fffffffb568)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/local/src/nsMsgBrkMBoxStore.cpp:270
#7  0x00007fffec52a4e0 in EwsIncomingServer::CreateFolderWithDetails (this=0x7fffacc4e4c0, id=..., parentId=<optimized out>, name=..., flags=4)
    at /home/babolivier/Documents/mozilla/mozilla-central/comm/mailnews/protocols/ews/EwsIncomingServer.cpp:133
#8  0x00007fffedbedb1c in xpcom::interfaces::idl::IEwsFolderCallbacks::Create (self=0x7fffb1b1a5c0, id=0x7fffffffb7f0, parentId=0x7fffffffb7d0, name=0x7fffffffb9e0, flags=4294947076)
    at /home/babolivier/Documents/mozilla/mozilla-central/obj-x86_64-pc-linux-gnu/dist/xpcrs/rt/IEwsClient.rs:159
#9  ews_xpcom::client::{impl#0}::push_sync_state_to_ui::{async_fn#0} () at ews_xpcom/src/client.rs:266
#10 ews_xpcom::client::{impl#0}::sync_folder_hierarchy_inner::{async_fn#0} () at ews_xpcom/src/client.rs:128
#11 0x00007fffedbe9c79 in ews_xpcom::client::{impl#0}::sync_folder_hierarchy::{async_fn#0} () at ews_xpcom/src/client.rs:47
```

(note: both stack traces have been truncated to stop right before getting into the async runtime, which isn't relevant here since I've verified both represent the same call to `IEwsFolderCallbacks::Create`)

I need to dig more into this in order to understand why this is happening (and how best to fix it).