usememos / memos

An open source, lightweight note-taking service. Easily capture and share your great thoughts.
https://usememos.com
MIT License
30.31k stars 2.27k forks source link

Leaving a comment via the API crashes memos #3254

Closed erauner12 closed 5 months ago

erauner12 commented 5 months ago

Describe the bug

The problem is that anytime I try to leave a comment via the API, it actually crashes the memos instance:

Error sending request: Error: Request failed, status 502
    at new t (app.js:1:1977769)
    at qG (app.js:1:1977961)
    at app.js:1:1978638
    at app.js:1:237056
    at Object.next (app.js:1:237161)
    at a (app.js:1:235879)

I have to start the container back up after each time that it happens.

I was following this:

https://memos.apidocumentation.com/reference#tag/memoservice/post/api/v2/{name}/comments

Steps to reproduce

{
  "url": "https://<host>/api/v2/memos/3/comments",
  "method": "POST",
  "headers": {
    "Authorization": "Bearer <redacted>"
  },
  "body": "{\"comment\":{\"content\":\"test\",\"visibility\":\"VISIBILITY_UNSPECIFIED\"}}"
}
{
  "comment": {
    "content": "",
    "visibility": "VISIBILITY_UNSPECIFIED"
  }
}
    async createMemoComment(memoName, commentContent, visibility = "VISIBILITY_UNSPECIFIED") {
        const url = `${this.API_URL_BASE}/${memoName}/comments`;
        const headers = this.getAuthenticationHeaders();
        const body = {
            comment: {
                content: commentContent,
                visibility: visibility,
            },
        };
        const response = await this.sendRequest(url, {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(body),
        });
        this.ensureValidResponse(response);
        console.log('Comment created successfully');
        return response.json.memo;
    }

Easy way to re-create

curl --request POST \
  --url https://<host>/api/v2/memos/3/comments \
  --header "Authorization: Bearer <redacted>" \
  --data '{
  "comment": {
    "content": "test",
    "visibility": "VISIBILITY_UNSPECIFIED"
  }
}'

Error

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

goroutine 72 [running]:
github.com/usememos/memos/server/route/api/v2.(*APIV2Service).CreateMemo(0xc000404630, {0x1963868, 0xc000531350}, 0x0)
    /backend-build/server/route/api/v2/memo_service.go:42 +0x66
github.com/usememos/memos/server/route/api/v2.(*APIV2Service).CreateMemoComment(0xc000404630, {0x1963868, 0xc000531350}, 0xc000125a40)
    /backend-build/server/route/api/v2/memo_service.go:340 +0x157
github.com/usememos/memos/proto/gen/api/v2._MemoService_CreateMemoComment_Handler.func1({0x1963868?, 0xc000531350?}, {0x16062e0?, 0xc000125a40?})
    /backend-build/proto/gen/api/v2/memo_service_grpc.pb.go:562 +0xcb
github.com/usememos/memos/server/route/api/v2.(*GRPCAuthInterceptor).AuthenticationInterceptor(0xc0003760f0, {0x1963868, 0xc0004c6330}, {0x16062e0, 0xc000125a40}, 0xc0000ea2a0, 0xc0003762e8)
    /backend-build/server/route/api/v2/acl.go:80 +0x33c
google.golang.org/grpc.getChainUnaryHandler.func1({0x1963868, 0xc0004c6330}, {0x16062e0, 0xc000125a40})
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1205 +0xb2
github.com/usememos/memos/server/route/api/v2.(*LoggerInterceptor).LoggerInterceptor(0x2487ae0, {0x1963868, 0xc0004c6330}, {0x16062e0?, 0xc000125a40?}, 0xc0000ea2a0, 0x18?)
    /backend-build/server/route/api/v2/logger_interceptor.go:20 +0x4e
google.golang.org/grpc.NewServer.chainUnaryServerInterceptors.chainUnaryInterceptors.func1({0x1963868, 0xc0004c6330}, {0x16062e0, 0xc000125a40}, 0xc0000ea2a0, 0x78?)
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1196 +0x85
github.com/usememos/memos/proto/gen/api/v2._MemoService_CreateMemoComment_Handler({0x16d29c0, 0xc000404630}, {0x1963868, 0xc0004c6330}, 0xc000164000, 0xc0002b3ba0)
    /backend-build/proto/gen/api/v2/memo_service_grpc.pb.go:564 +0x143
google.golang.org/grpc.(*Server).processUnaryRPC(0xc000233400, {0x1963868, 0xc0004c6270}, {0x196d1c0, 0xc0004a6680}, 0xc000398a20, 0xc000404900, 0x2411b28, 0xc0001257c0)
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1385 +0xdd1
google.golang.org/grpc.(*Server).handleStream(0xc000233400, {0x196d1c0, 0xc0004a6680}, 0xc000398a20)
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1796 +0xfb8
google.golang.org/grpc.(*Server).serveStreams.func2.1()
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1029 +0x8b
created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 57
    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1040 +0x125

The version of Memos you're using

Version: v0.21.0 (stable image tag)

Screenshots or additional context

No response

boojack commented 5 months ago
curl --request POST \
  --url https://memos.apidocumentation.com/api/v2/__NAME__/comments \
  --data '{
  "comment": {
    "content": "",
    "visibility": "VISIBILITY_UNSPECIFIED"
  }
}'

https://memos.apidocumentation.com/reference#tag/memoservice/post/api/v2/{name}/comments

erauner12 commented 5 months ago

Hi @boojack

I'm still unclear about the resolution. I hope you can help me understand.

In this example, I need to fetch a specific memo and leave a comment on it. Here's how I'm constructing the GET and POST requests.

GET Request:

curl --request GET \
  --url https://memos.apidocumentation.com/api/v2/__NAME_3__
HOST="<host>"
TOKEN="<token>"
MEMO_NAME="memos/90"

curl --request GET \
 --url https://$HOST/api/v2/$MEMO_NAME \
 -H "Authorization: Bearer $TOKEN" \
 | jq

This fetches a memo with the name memos/90 . It seems to work as expected, returning the correct data:
 

{
 "memo": {
   "name": "memos/90",
   "uid": "TCdDSCEPHS2V9SgbouDMeB",
   "rowStatus": "ACTIVE",
   "creator": "users/1",
   "createTime": "2024-04-22T16:15:12Z",
   "updateTime": "2024-04-22T16:15:12Z",
   "displayTime": "2024-04-22T16:15:12Z",
   "content": "test",
   "visibility": "PRIVATE",
   "pinned": false,
   "resources": [],
   "relations": [],
   "reactions": []
 }
}

However, when I try to post a comment on this particular memo, I construct the POST request as follows:

curl --request POST \
  --url https://memos.apidocumentation.com/api/v2/__NAME__/comments \
  --data '{
  "comment": {
    "content": "",
    "visibility": "VISIBILITY_UNSPECIFIED"
  }
}'

POST Request:

HOST="<host>"
TOKEN="<token>"
MEMO_NAME="memos/90"

curl --request POST \
 --url https://$HOST/api/v2/$MEMO_NAME/comments \
 -H "Authorization: Bearer $TOKEN" \
 --data '{
   "comment": {
     "content": "test comment",
     "visibility": "VISIBILITY_UNSPECIFIED"
   }
 }'

I'm unsure if I'm passing the correct memo name in this request. The target URL is constructed like this: https://$HOST/api/v2/memos/90/comments. When I send this post request, my server crashes?

Could you confirm if this structure is correct, or point out any errors? Thank you for your assistance.

erauner12 commented 5 months ago

I think the server might be expecting a name or something:

2024/04/26 03:30:21 ERROR CreateMemoComment request request="name:\"memos/18\" comment:{content:\"testsfasdfasdf\" visibility:PRIVATE}"

this works fine when sent from the web app, but if you try to send it via API request. That's when it crashes the server.

...

Got the development container running, but I could figure out how to reproduce the error with a curl from my local, kept getting errors.

danwinkler commented 5 months ago

I'm also seeing this behavior. Attempting to add a comment through the API takes down the server.

memos  | panic: runtime error: invalid memory address or nil pointer dereference
memos  | [signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0xc5aa6c]
memos  |
memos  | goroutine 298 [running]:
memos  | github.com/usememos/memos/server/route/api/v2.(*APIV2Service).CreateMemo(0x4000428780, {0x1435568, 0x400063d8f0}, 0x0)
memos  |    /backend-build/server/route/api/v2/memo_service.go:42 +0x4c
memos  | github.com/usememos/memos/server/route/api/v2.(*APIV2Service).CreateMemoComment(0x4000428780, {0x1435568, 0x400063d8f0}, 0x400062b000)
memos  |    /backend-build/server/route/api/v2/memo_service.go:340 +0x118
memos  | github.com/usememos/memos/proto/gen/api/v2._MemoService_CreateMemoComment_Handler.func1({0x1435568?, 0x400063d8f0?}, {0x10d5060?, 0x400062b000?})
memos  |    /backend-build/proto/gen/api/v2/memo_service_grpc.pb.go:562 +0xd0
memos  | github.com/usememos/memos/server/route/api/v2.(*GRPCAuthInterceptor).AuthenticationInterceptor(0x4000418138, {0x1435568, 0x400063d740}, {0x10d5060, 0x400062b000}, 0x400007e8a0, 0x4000680e70)
memos  |    /backend-build/server/route/api/v2/acl.go:80 +0x320
memos  | google.golang.org/grpc.getChainUnaryHandler.func1({0x1435568, 0x400063d740}, {0x10d5060, 0x400062b000})
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1205 +0xa0
memos  | github.com/usememos/memos/server/route/api/v2.(*LoggerInterceptor).LoggerInterceptor(0x1f35ca0, {0x1435568, 0x400063d740}, {0x10d5060?, 0x400062b000?}, 0x400007e8a0, 0x18?)
memos  |    /backend-build/server/route/api/v2/logger_interceptor.go:20 +0x4c
memos  | google.golang.org/grpc.NewServer.chainUnaryServerInterceptors.chainUnaryInterceptors.func1({0x1435568, 0x400063d740}, {0x10d5060, 0x400062b000}, 0x400007e8a0, 0x113ff40?)
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1196 +0x88
memos  | github.com/usememos/memos/proto/gen/api/v2._MemoService_CreateMemoComment_Handler({0x11a1660, 0x4000428780}, {0x1435568, 0x400063d740}, 0x4000630680, 0x400036bb80)
memos  |    /backend-build/proto/gen/api/v2/memo_service_grpc.pb.go:564 +0x148
memos  | google.golang.org/grpc.(*Server).processUnaryRPC(0x40002fee00, {0x1435568, 0x400063d680}, {0x143eec0, 0x40003861a0}, 0x4000646240, 0x4000428a50, 0x1eb9ba8, 0x400062afc0)
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1385 +0xb40
memos  | google.golang.org/grpc.(*Server).handleStream(0x40002fee00, {0x143eec0, 0x40003861a0}, 0x4000646240)
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1796 +0xc00
memos  | google.golang.org/grpc.(*Server).serveStreams.func2.1()
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1029 +0x8c
memos  | created by google.golang.org/grpc.(*Server).serveStreams.func2 in goroutine 48
memos  |    /go/pkg/mod/google.golang.org/grpc@v1.61.1/server.go:1040 +0x13c
memos exited with code 2
erauner12 commented 4 months ago

@boojack Can we reopen this issue? Another person above also encountered the same error, suggesting it's not caused by calling the memoservice incorrectly. Let me know if you need more details, and I can provide them.