I managed to fix the issue by replacing the problematic file with another one from another music library.

I asked the Music app to create a new library (by holding the option key while opening the app). I have an Apple Music subscription and I have my music files uploaded to the cloud. I looked into the library file by clicking on "show package contents" and then copied Application.musicdb (the file that causes the issue in my main library ) into my main library to replace the problematic file. Then I closed the Music app and opened my main library and everything worked fine. I also deleted all the temp files and the app has been working fine since then.

Hope this helps others with the same problem.