Skip to content

Http-go template fails #820

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

Closed
mikkelhegn opened this issue Oct 14, 2022 · 31 comments
Closed

Http-go template fails #820

mikkelhegn opened this issue Oct 14, 2022 · 31 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@mikkelhegn
Copy link
Contributor

Creating a new Spin application using the http-go template fails without any modifications to the code:

Serving http://127.0.0.1:3000
Available Routes:
  go-temp2: http://127.0.0.1:3000 (wildcard)
2022-10-14T17:31:26.421024Z ERROR spin_http_engine: Error processing request: wasm trap: wasm `unreachable` instruction executed
wasm backtrace:
    0: 0x3a8f - <unknown>!runtime.runtimePanic
@radu-matei radu-matei added bug Something isn't working help wanted Extra attention is needed labels Oct 16, 2022
@radu-matei
Copy link
Member

Could you please share which versions for Spin and TinyGo this is?

Just validated this with Spin from canary, TinyGo 0.26, and the latest template version and it fails for me as well.

For reference, I also tested our C# template, which is built on top of the same C interop, and that one works well.

cc @adamreese

@etehtsea
Copy link
Contributor

etehtsea commented Oct 16, 2022

As it passes on CI, it looks like there are some breaking changes between 0.22 and 0.26 - https://github.com/fermyon/spin/actions/runs/3245485308/jobs/5323079898

This is from the log on CI (0.22):

make -C sdk/go test
make[1]: Entering directory '/home/runner/work/spin/spin/sdk/go'
tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o http/testdata/http-tinygo/main.wasm http/testdata/http-tinygo/main.go
go test -v .
=== RUN   TestHTTPTriger
    integration_test.go:76: building example:  http/testdata/http-tinygo
    integration_test.go:80: Get "http://127.0.0.1:35811/hello": dial tcp 127.0.0.1:35811: connect: connection refused
    integration_test.go:80: Get "http://127.0.0.1:35811/hello": dial tcp 127.0.0.1:35811: connect: connection refused
    integration_test.go:80: Get "http://127.0.0.1:35811/hello": dial tcp 127.0.0.1:35811: connect: connection refused
    integration_test.go:80: Get "http://127.0.0.1:35811/hello": dial tcp 127.0.0.1:35811: connect: connection refused
    integration_test.go:85: 200 OK
--- PASS: TestHTTPTriger (21.91s)
=== RUN   TestBuildExamples
    integration_test.go:114: building example:  ../../examples/config-tinygo
    integration_test.go:114: building example:  ../../examples/http-tinygo
    integration_test.go:114: building example:  ../../examples/http-tinygo-outbound-http
    integration_test.go:114: building example:  ../../examples/tinygo-outbound-redis
    integration_test.go:114: building example:  ../../examples/tinygo-redis
--- PASS: TestBuildExamples (43.40s)
PASS
ok  	github.com/fermyon/spin/sdk/go	65.313s
tinygo test -wasm-abi=generic -target=wasi -gc=leaking -v ./http
?   	github.com/fermyon/spin/sdk/go/http	[no test files]
tinygo test -wasm-abi=generic -target=wasi -gc=leaking -v ./redis
?   	github.com/fermyon/spin/sdk/go/redis	[no test files]
make[1]: Leaving directory '/home/runner/work/spin/spin/sdk/go'

@mikkelhegn
Copy link
Contributor Author

I've been testing the 0.5.0 release and the canary release (from October 14th) and both fail, with both SDK, Spin runtime, and template being from the same version.

TinyGo: tinygo version 0.26.0 darwin/amd64 (using go version go1.19.2 and LLVM version 14.0.0)

@etehtsea
Copy link
Contributor

etehtsea commented Oct 17, 2022

I did a little research about it - it works fine up to 0.25.0 (incl).
Also, I've figured out the commit that is causing the issue - tinygo-org/tinygo@03d1c44
Reverting it from the tinygo's dev branch make tests (make test-sdk-go) to pass again (so as http-tinygo example).

Unfortunately, I'm unfamiliar with the internals and can't tell if it's the upstream bug or not without further debugging.

UPD: This in-progress PR seems to fix the issue so now it looks more like an upstream bug

@radu-matei
Copy link
Member

radu-matei commented Oct 18, 2022

Validated that TinyGo v0.25 works.
I think we should update the documentation to point out the version we know works.

Thanks for investigating this!

@lann
Copy link
Collaborator

lann commented Oct 25, 2022

Removing -no-debug from the tinygo build command and running spin with WASMTIME_BACKTRACE_DETAILS=1 gives more detail:

    2: 0x141d9 - realloc
                    at /usr/lib/tinygo/src/runtime/arch_tinygowasm.go:136:28

@vdice vdice moved this to 🆕 Triage Needed in Spin Triage Oct 25, 2022
@lann
Copy link
Collaborator

lann commented Oct 25, 2022

Finally getting back to this... The backtrace appears to point at the indexing operation here:

ptr := unsafe.Pointer(&buf[0])

Suggesting that the panic could be caused by buf having size zero, which could possibly happen if realloc was called with size 0.

@vdice vdice moved this from 🆕 Triage Needed to 🏗 In progress in Spin Triage Oct 25, 2022
@vdice vdice moved this from 🏗 In progress to 🔖 Backlog in Spin Triage Oct 25, 2022
@lann
Copy link
Collaborator

lann commented Oct 25, 2022

This makes sense with the patch identified in #820 (comment), which changed the underlying realloc implementation. I'm testing a patch to tinygo.

@lann
Copy link
Collaborator

lann commented Nov 22, 2022

Almost certainly fixed by tinygo-org/tinygo#3303

@adamreese
Copy link
Contributor

When I patch tinygo with #3303 I get a different error

    0: 0x2a9ec - abort
                    at /Users/areese/p/src/github.com/tinygo-org/tinygo/lib/wasi-libc/libc-bottom-half/sources/abort.c:5:5
    1: 0x29b8f - canonical_abi_realloc
                    at /Users/areese/p/src/github.com/fermyon/spin/sdk/go/http/wasi-outbound-http.c:13:3

Looks like the bindings expect a pointer returned from realloc but the patch returns nil.
https://github.com/fermyon/spin/blob/722107325f01a58e5e04b09df3e70f73409c58bf/sdk/go/http/wasi-outbound-http.c#L11-L13
https://github.com/tinygo-org/tinygo/pull/3303/files#diff-5134aa362b4882a502f819967129e9d01184206daf02f989e50aa03999632ae6R45-R49

@lann
Copy link
Collaborator

lann commented Jan 23, 2023

realloc(ptr, 0) is "allowed" to return null by POSIX, but glibc returns a unique pointer and it looks like wit-bindgen depends on that behavior.

@tomaswarynyca
Copy link

tomaswarynyca commented Jan 24, 2023

Hi, any idea of whats is happening?

2023-01-24T11:16:24.608075Z ERROR spin_http: Error processing request: error while executing at wasm backtrace:
    0: 0x3a4a - <unknown>!runtime.runtimePanic
    1:  0x830 - <unknown>!runtime.lookupPanic
    2: 0xac0f - <unknown>!realloc
    3: 0x27ce0 - <unknown>!canonical_abi_realloc

Caused by:
    wasm trap: wasm `unreachable` instruction executed

OS: Fedora 37
TinyGo: 0.26.0
Spin: 0.7.1 (6cf7447 2022-12-19)

@mikkelhegn
Copy link
Contributor Author

Can you try TinyGo 0.25.0? 0.26.0 has an issue, but haven't been fully investigated yet.

@tomaswarynyca
Copy link

Same problem with TinyGo 0.25.0!

@mikkelhegn
Copy link
Contributor Author

I cannot replicate, can you share the code? I was using the http-go template. I'm on MacOS.

Screenshot 2023-01-24 at 13 06 57

@tomaswarynyca
Copy link

tomaswarynyca commented Jan 24, 2023

➜ tinygo version
tinygo version 0.25.0 linux/amd64 (using go version go1.19.5 and LLVM version 14.0.5)
➜ spin -V       
spin 0.7.1 (6cf7447 2022-12-19)
➜ spin new http-go go-0.25
Project description: 
HTTP base: /
HTTP path: /...
➜ cd go-0.25
➜ spin build              
Executing the build command for component go-0-25: tinygo build -wasm-abi=generic -target=wasi -gc=leaking -no-debug -o main.wasm main.go
# github.com/fermyon/spin/sdk/go/http
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/__struct_iovec.h:5:10: fatal: 'stddef.h' file not found
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/internals.go:4:12: note: in file included from internals.go!cgo.c:3:
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:8:12: note: in file included from /home/fedora/go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:8:
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/stdint.h:20:10: note: in file included from /usr/lib64/tinygo/lib/wasi-libc/sysroot/include/stdint.h:20:
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/bits/alltypes.h:402:10: note: in file included from /usr/lib64/tinygo/lib/wasi-libc/sysroot/include/bits/alltypes.h:402:
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:62:24: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:12:11: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:13:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:63:21: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:25:24: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:26:24: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:30:39: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:31:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:64:25: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:50:39: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:51:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:65:24: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:57:10: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:20:14: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:21:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:58:22: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:66:29: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:75:29: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:70:10: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:71:25: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:76:32: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/spin-http.h:77:29: expected a bitfield
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/__struct_iovec.h:5:10: fatal: 'stddef.h' file not found
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/outbound_internals.go:10:13: note: in file included from outbound_internals.go!cgo.c:3:
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:8:12: note: in file included from /home/fedora/go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:8:
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/stdint.h:20:10: note: in file included from /usr/lib64/tinygo/lib/wasi-libc/sysroot/include/stdint.h:20:
../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/bits/alltypes.h:402:10: note: in file included from /usr/lib64/tinygo/lib/wasi-libc/sysroot/include/bits/alltypes.h:402:
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:62:33: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:12:11: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:13:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:63:30: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:25:33: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:26:33: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:30:48: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:31:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:64:34: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:50:48: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:51:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:65:33: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:57:10: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:20:14: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:21:12: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:58:31: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:66:38: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:75:38: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:70:10: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:71:34: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:76:41: expected a bitfield
../../go/pkg/mod/github.com/fermyon/spin/sdk/[email protected]/http/wasi-outbound-http.h:77:38: expected a bitfield
Error: Build command for component go-0-25 failed with status Exited(1).

@mikkelhegn
Copy link
Contributor Author

@dicej / @lann any idea?

@lann
Copy link
Collaborator

lann commented Jan 24, 2023

@tomaswarynyca You may need to install clang?

@tomaswarynyca
Copy link

@lann I have it installed

➜ clang --version
clang version 15.0.7 (Fedora 15.0.7-1.fc37)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

@lann
Copy link
Collaborator

lann commented Jan 24, 2023

Did you install Tinygo as a package? I am not too familiar with Fedora.

@tomaswarynyca
Copy link

tomaswarynyca commented Jan 24, 2023

Yeah, as it says on the TinyGo website
https://tinygo.org/getting-started/install/linux/#fedora-linux

image

Installing TinyGo 0.25 installs a few more dependencies

image

@lann
Copy link
Collaborator

lann commented Jan 24, 2023

../../../../usr/lib64/tinygo/lib/wasi-libc/sysroot/include/__struct_iovec.h:5:10: fatal: 'stddef.h' file not found

The most obvious interpretation of this is a missing package. From a quick search the only other one that makes sense to me for Fedora might be kernel-headers.

@lann
Copy link
Collaborator

lann commented Jan 24, 2023

tinygo-org/tinygo#3410 should fix the original error, though it revealed another issue with the SDK bindings as well so it isn't a complete fix.

@lann
Copy link
Collaborator

lann commented Feb 6, 2023

#1114 might have fixed the issue with tinygo 0.26.

@mikkelhegn
Copy link
Contributor Author

Hurrah!

image

For anyone wanting this upgrade. Spin v0.8.0 works, but you need to update the Go SDK reference using go get github.com/fermyon/spin/sdk/go@main in the folder containing go.mod for your Spin component.

@mikkelhegn
Copy link
Contributor Author

I'd suggest to keep this issue open until we have a v0.9.0 version shipped, and this fix will be part of the default SDK version being used by the template.

@vdice
Copy link
Contributor

vdice commented Feb 15, 2023

With tinygo 0.27.0 although the http-go template and http-tinygo example each build/run successfully for me, I seem to get errors for the other tinygo-related templates and example apps. Others able to confirm? (Not sure if user error)

For example, the http-tinygo-outbound-http example (here using spin and sdk from #1153, which is current with main as of writing):

$ spin -V
spin 0.8.0 (ca0b046 2023-02-15)

$ spin build
Executing the build command for component tinygo-hello: tinygo build -target=wasi -gc=leaking -no-debug -o main.wasm main.go
spin up
Successfully ran the build command for the Spin components.

$ spin up
Serving http://127.0.0.1:3000
Available Routes:
  tinygo-hello: http://127.0.0.1:3000/hello
panic: free: invalid pointer
2023-02-15T22:01:07.166089Z ERROR spin_http: Error processing request: error while executing at wasm backtrace:
    0: 0x196b - <unknown>!runtime._panic.llvm.6707981987192336823
    1: 0x6530 - <unknown>!free
    2: 0x2cc9e - <unknown>!canonical_abi_free
    3: 0x2ce6b - <unknown>!spin_http_headers_free
    4: 0x2d1c3 - <unknown>!spin_http_request_free
    5: 0x2c98a - <unknown>!github.com/fermyon/spin/sdk/go/http.send$1
    6: 0x2c80e - <unknown>!github.com/fermyon/spin/sdk/go/http.send
    7: 0x2a51e - <unknown>!spin_http_handle_http_request
    8: 0x2d338 - <unknown>!__wasm_export_spin_http_handle_http_request

Caused by:
    wasm trap: wasm `unreachable` instruction executed

If someone who is still on tinygo 0.26.0 can compare notes, that'd be handy. If it is indeed particular to 0.27.0, I/we can create a separate issue.

@dicej
Copy link
Contributor

dicej commented Feb 15, 2023

That could be a bug in the SDK that managed to stay hidden with earlier tinygo versions. I'm working on updating the Go SDK right now to add key-value support and update outbound-redis. I can take a look at this while I'm at it.

@lann
Copy link
Collaborator

lann commented Feb 15, 2023

This does look vaguely like the kind of bug I (thought I) fixed.

@dicej
Copy link
Contributor

dicej commented Feb 16, 2023

This should be addressed in #1165

@vdice
Copy link
Contributor

vdice commented Feb 17, 2023

With the recent v0.9.0 release (and corresponding go SDK), compatibility with tinygo should be restored.

Thanks @dicej @lann @adamreese for the fixes!

@vdice vdice closed this as completed Feb 17, 2023
@github-project-automation github-project-automation bot moved this from 🔖 Backlog to ✅ Done in Spin Triage Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

8 participants