Questions on S3 Backend: State Update Timing & Recovery from Interrupted 'apply'

Hello everyone,

I have two specific questions about how the S3 backend handles state during an apply operation, especially regarding timing and failure recovery.

  1. When using the S3 backend, at what exact point during the terraform apply process is the state file in the S3 bucket actually updated? (For example, is it updated incrementally as each resource is provisioned, or only written once at the very end after all resources are successfully applied?)

  2. What happens if the terraform apply process is interrupted, exits abnormally, or is killed while it is in the middle of applying changes? Is it possible to end up in a situation where some resources were successfully created in the cloud but are not recorded in the S3 state file? If this happens, what is the recommended recovery process to fix this and get the state file back in sync with reality?

Thank you in advance for any insights!