Replace the function "libpd_savefile" with "libpd_request_savefile" and "libpd_wait_until_file_is_saved"