Question about execution of plugins


I’ve been trying to get an understanding of how terraform (and the SDK) execute plugins.

I know that go-plugin is used to execute the plugins and then gRPC is used to communicate with the plugin to run the various CRUD operations.

I believe that terraform executes 1 process per plugin only although it seems like it starts the plugins mutliple times during an apply before it gets to the state of doing any mutations. I assume this is because it runs a process during state refresh (and other earlier parts of the DAG traversal etc), which then exits before the actual apply stage is evaluated but I would like to know for sure what is happening.

The ultimate reason I would like to know, is that I currently believe that using a sync.Mutex to protect a resource in our provider that cannot handle simultaneous updates is a valid approach (to my knowledge this is what both the AWS and Google providers do). If multiple instances of the plugin are run during the actual apply stage then obviously a sync.Mutex would not actually achieve to serialize the updates to the resource as they would be running in isolated processes.

Appreciate any input on this.