hashicorp / terraform-provider-awscc

Terraform AWS Cloud Control provider
https://registry.terraform.io/providers/hashicorp/awscc/latest/docs
Mozilla Public License 2.0
249 stars 114 forks source link

plugin6.(*GRPCProvider).ApplyResourceChange request, invalid memory address or nil pointer dereference #1480

Open titus-chu-kmon opened 6 months ago

titus-chu-kmon commented 6 months ago

hi, i got this when trying to create a DMS data provider

awscc_dms_data_provider.target: Creating...
╷
│ Error: Request cancelled
│ 
│ The plugin6.(*GRPCProvider).ApplyResourceChange request was cancelled.
╵

Stack trace from the terraform-provider-awscc_v0.71.0_x5 plugin:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0xae2a72]

goroutine 39 [running]:
github.com/hashicorp/terraform-provider-awscc/internal/generic.(*genericResource).Create(0xc0015e2fc0, {0x2cb79e8?, 0xc00365d290?}, {{{{0x2cbc838, 0xc0039424b0}, {0x1dfdde0, 0xc00391cb70}}, {0x2ce2338, 0xc001e5b0e0}}, {{{0x2cbc838, ...}, ...}, ...}, ...}, ...)
        github.com/hashicorp/terraform-provider-awscc/internal/generic/resource.go:416 +0x7b2
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0xc000372ea0, {0x2cb79e8, 0xc00365d290}, 0xc002ad7318, 0xc002ad72b8)
        github.com/hashicorp/terraform-plugin-framework@v1.5.0/internal/fwserver/server_createresource.go:101 +0x578
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0xc001963470?, {0x2cb79e8, 0xc00365d290}, 0xc001f80c30, 0xc002ad7470)
        github.com/hashicorp/terraform-plugin-framework@v1.5.0/internal/fwserver/server_applyresourcechange.go:57 +0x4a5
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ApplyResourceChange(0xc000372ea0, {0x2cb79e8?, 0xc00365d170?}, 0xc001f80b90)
        github.com/hashicorp/terraform-plugin-framework@v1.5.0/internal/proto6server/server_applyresourcechange.go:55 +0x3e5
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ApplyResourceChange(0xc0002f5720, {0x2cb79e8?, 0xc00365c780?}, 0xc000e0e4d0)
        github.com/hashicorp/terraform-plugin-go@v0.20.0/tfprotov6/tf6server/server.go:857 +0x56b
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ApplyResourceChange_Handler({0x1f09a00?, 0xc0002f5720}, {0x2cb79e8, 0xc00365c780}, 0xc001f97980, 0x0)
        github.com/hashicorp/terraform-plugin-go@v0.20.0/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:503 +0x169
google.golang.org/grpc.(*Server).processUnaryRPC(0xc00027e960, {0x2cb79e8, 0xc00365c6f0}, {0x2ce0060, 0xc0001024e0}, 0xc0024f98c0, 0xc000451aa0, 0x3a6a758, 0x0)
        google.golang.org/grpc@v1.60.0/server.go:1372 +0xe03
google.golang.org/grpc.(*Server).handleStream(0xc00027e960, {0x2ce0060, 0xc0001024e0}, 0xc0024f98c0)
        google.golang.org/grpc@v1.60.0/server.go:1783 +0xfec
google.golang.org/grpc.(*Server).serveStreams.func2.1()
        google.golang.org/grpc@v1.60.0/server.go:1016 +0x59
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 36
        google.golang.org/grpc@v1.60.0/server.go:1027 +0x115

Error: The terraform-provider-awscc_v0.71.0_x5 plugin crashed!

This is always indicative of a bug within the plugin. It would be immensely
helpful if you could report the crash with the plugin's maintainers so that it
can be fixed. The output above should help diagnose the issue.
quixoticmonk commented 1 week ago

@titus-chu-kmon has this occurred again with the later versions of the provider ?

thisdougb commented 1 week ago

Hi,

I'm seeing this with v1.14.0_x5. I checked with the above tag, my panic at the statement on line 397 of resource.go matches the statement in the older version of the file.

Resources being created:

aws_backup_region_settings.r1 Modifying
awscc_backup_restore_testing_plan.s1[0] Creating...
aws_backup_region_settings.r1 complete after 1s
aws_backup_region_settings.r2 Modifying
awscc_backup_restore_testing_plan.s2[0] Creating...
aws_backup_region_settings.r2 complete after 0s

Error: Plugin did not respond

I guess the progressEvent.Identifier is invalid, and not checked before use. Both Go routines doing the Create panic'd.

My crash happens on line 397:

id := aws.ToString(progressEvent.Identifier)

Sorry this is for a client, so I can't share much. But happy to work on a fix.

thisdougb commented 6 days ago

Here's a possible fix, just a check that the string pointer isn't nil. In the error clause it's already checking for an empty string, as a not-created-resource is possible at this point.

        var progressEvent *cctypes.ProgressEvent
        waiter := cloudcontrol.NewResourceRequestSuccessWaiter(conn, func(o *cloudcontrol.ResourceRequestSuccessWaiterOptions) {
                o.Retryable = tfcloudcontrol.RetryGetResourceRequestStatus(&progressEvent)
        })

        err = waiter.Wait(ctx, &cloudcontrol.GetResourceRequestStatusInput{RequestToken: output.ProgressEvent.RequestToken}, r.createTimeout)

Insert a check on the string pointer here:

        // If the resource failed to create, then Identifier may be a nil ptr
        var id string
        if progressEvent.Identifier != nil {
                id = aws.ToString(progressEvent.Identifier)
        }
        if err != nil {
                response.Diagnostics.Append(ServiceOperationWaiterErrorDiag("Cloud Control API", "CreateResource", err))

                // Save any ID to state so that the resource will be marked as tainted.
                if id != "" {
                        if err := r.setId(ctx, id, &response.State); err != nil {
                                response.Diagnostics.Append(ResourceIdentifierNotSetDiag(err))
                        }
                }

                return
        }

The id var is used later in the function, so needs to be initialised outside the error clause.

thisdougb commented 5 hours ago

@quixoticmonk is there a way to raise attention on the PR for this, to maybe progress to a review?

Entirely selfishly we could do with a (this) bug fix. 😄