Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tflock crashes in the latest 1.x versions of terraform #9

Open
kishaningithub opened this issue Jan 17, 2023 · 12 comments
Open

tflock crashes in the latest 1.x versions of terraform #9

kishaningithub opened this issue Jan 17, 2023 · 12 comments

Comments

@kishaningithub
Copy link

kishaningithub commented Jan 17, 2023

When i run tflock on terraform scripts i get the following segmentation fault error

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x101143eec]

goroutine 1 [running]:
github.com/hashicorp/terraform/command/views.(*StateLockerHuman).Locking(0x14000061450?)
	/Users/kishanb/go/pkg/mod/github.com/hashicorp/terraform@v0.15.1/command/views/state_locker.go:35 +0x1c
github.com/hashicorp/terraform/internal/helper/slowmessage.Do(0x102a5b890?, 0x140006f2a00, 0x1400099fcb8)
	/Users/kishanb/go/pkg/mod/github.com/hashicorp/terraform@v0.15.1/internal/helper/slowmessage/slowmessage.go:29 +0xec
github.com/hashicorp/terraform/command/clistate.(*locker).Lock(0x140000613b0, {0x12b7e0f58?, 0x1400094be00}, {0x1020bec6f, 0x6})
	/Users/kishanb/go/pkg/mod/github.com/hashicorp/terraform@v0.15.1/command/clistate/state.go:120 +0x248
main.(*LockCommand).Run(0x1400017c700, {0x0?, 0x103a31bc0?, 0x14000164a80?})
	/Users/kishanb/Programming/Personal/tflock/main.go:40 +0x188
github.com/mitchellh/cli.(*CLI).Run(0x140000c8c80)
	/Users/kishanb/go/pkg/mod/github.com/mitchellh/cli@v1.1.2/cli.go:262 +0x4a8
main.main()
	/Users/kishanb/Programming/Personal/tflock/main.go:116 +0x318

Terraform version

$ terraform version
Terraform v1.3.7
on darwin_arm64

Simulation

To simulate run the tflock command and run terraform plan command for this sample terraform file after creating the state bucket and dynamodb table

terraform {
  backend "s3" {
    bucket = "state-bucket-cc5b487c-a7d8-4751-b436-43f50994e962"
    key    = "path/file"
    region = "us-east-2"
    encrypt = true
    dynamodb_table = "terraform-state-lock-table"
  }
}

resource "aws_s3_bucket" "spike_data_bucket" {
  bucket = "spike-data-bucket-cc5b487c-a7d8-4751-b436-43f50994e962"
}
@minamijoyo
Copy link
Owner

Thank you for reporting this, but I cannot reproduce the error.
I tested with the latest Terraform v1.3.7 and confirmed that it worked. There are some conditions that can be reproduced.

@kishaningithub
Copy link
Author

@minamijoyo I am still getting the above error(probably i am doing something wrong). From what i see the error as such comes out from the below line which is from within the hashicorp terraform library (screenshot below)

Screenshot 2023-01-30 at 8 09 58 PM

So either the view or streams is nil

Following the call stack i see the orgin call happening from here which is there in this repo

Screenshot 2023-01-30 at 8 12 01 PM

From the looks of it, do you see in what scenario this can happen?

@minamijoyo
Copy link
Owner

What OS are you working on? The stream implementation expects a Unix-like system, and some Windows environments are not fully supported.
https://github.com/hashicorp/terraform/blob/v0.15.1/internal/terminal/impl_windows.go

If so, changing the OS or terminal emulator may solve the problem.

If not, to further debug, you can use the replace directive in go.mod, which allows you to replace the hashicorp/terraform dependency to your local copy and inject some Printf statements for debugging.

@kishaningithub
Copy link
Author

I am running this on an M1 Mac Book Pro running the latest Mac OS Ventura version(13.2). M1 processor uses is arm64 architecture.

@n2N8Z
Copy link

n2N8Z commented Feb 19, 2023

Same error using 0.13.5 on linux_amd64
deleted ~/go and rebuilt, but same error.

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

goroutine 1 [running]:
github.com/hashicorp/terraform/command/views.(*StateLockerHuman).Locking(0xc0009065a0?)
        /home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/command/views/state_locker.go:35 +0x17
github.com/hashicorp/terraform/internal/helper/slowmessage.Do(0x2568fb0?, 0xc000cdc500, 0xc000c99ce0)
        /home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/internal/helper/slowmessage/slowmessage.go:29 +0x105
github.com/hashicorp/terraform/command/clistate.(*locker).Lock(0xc0009a2050, {0x7fab1277d0b8?, 0xc000903bc0}, {0x21320b2, 0x6})
        /home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/command/clistate/state.go:120 +0x269
main.(*LockCommand).Run(0xc0002808c0, {0x0?, 0xc000477cc0?, 0x0?})
        /home/redacted/tmp/tflock/main.go:40 +0x185
github.com/mitchellh/cli.(*CLI).Run(0xc000477cc0)
        /home/redacted/go/pkg/mod/github.com/mitchellh/[email protected]/cli.go:262 +0x5f8
main.main()
        /home/redacted/tmp/tflock/main.go:116 +0x2a5

@minamijoyo
Copy link
Owner

You can further debug by injecting some Printf statements, as I mentioned above.

@n2N8Z
Copy link

n2N8Z commented Feb 20, 2023

Can you suggest some places where printf statements would be useful, and of which variables ?

@minamijoyo
Copy link
Owner

I have no idea of a specific cause. Read the stack trace and find where the nil comes from and why. If you prefer a debugger with step execution, the delve debugger may be helpful.

@n2N8Z
Copy link

n2N8Z commented Mar 16, 2023

Downgrading github.com/hashicorp/[email protected] to github.com/hashicorp/[email protected] lets me lock the state file with Terraform 0.13.5.

Running tflock when the state file is already locked results in the same error as reported above.
$ ~/tmp/tflock/tflock
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb14a37]

goroutine 1 [running]:
github.com/hashicorp/terraform/command/views.(*StateLockerHuman).Locking(0xc0006804e0?)
/home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/command/views/state_locker.go:35 +0x17
github.com/hashicorp/terraform/internal/helper/slowmessage.Do(0x257dfa8?, 0xc0007c0340, 0xc000b1fcf0)
/home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/internal/helper/slowmessage/slowmessage.go:29 +0x105
github.com/hashicorp/terraform/command/clistate.(*locker).Lock(0xc000730690, {0x7f64d0693518?, 0xc0000648a0}, {0x21806de, 0x6})
/home/redacted/go/pkg/mod/github.com/hashicorp/[email protected]/command/clistate/state.go:120 +0x269
main.(*LockCommand).Run(0xc00059d340, {0x0?, 0xc00079fe01?, 0x40fb92?})
/home/redacted/tmp/tflock/main.go:40 +0x185
github.com/mitchellh/cli.(*CLI).Run(0xc00017a500)
/home/redacted/go/pkg/mod/github.com/mitchellh/[email protected]/cli.go:262 +0x5f8
main.main()
/home/redacted/tmp/tflock/main.go:116 +0x2a5

@aanderse
Copy link

aanderse commented Jun 6, 2023

Does anyone have any additional information on this? I run into this issue on some machines but not others.

@n2N8Z
Copy link

n2N8Z commented Jun 6, 2023

For me it actually works sometimes and not others, so some temporal, or race condition is going on. Try running it several times, on the ones where it crashed.
This is not a solution obviously.

@aanderse
Copy link

aanderse commented Jun 9, 2023

Interesting. For me the tool either consistently works or consistently crashes, depending on the machine I run the tool on, no matter how many times I try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants