Receipt-Wrangler / receipt-wrangler-api

Server for Receipt Wrangler
GNU Affero General Public License v3.0
77 stars 0 forks source link

http: panic serving 172.19.0.4:57348: runtime error: invalid memory address or nil pointer dereference #163

Open wes1993 opened 11 months ago

wes1993 commented 11 months ago

Hello, Thanks for this wonderful project, unfortunately i have one problem.... When i try to add a new receipt and use OCR (OpenAI) i have this errors:

receipt-wrangler-proxy-1     | 2023/12/04 14:56:03 [warn] 29#29: *37 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000005, client: 192.168.0.199, server: , request: "POST /api/receipt/quickScan HTTP/1.1", host: "192.168.0.113:9082", referrer: "http://192.168.0.113:9082/receipts/group/2"
receipt-wrangler-api-1       | 2023/12/04 14:56:26 http: panic serving 172.19.0.4:57348: runtime error: invalid memory address or nil pointer dereference
receipt-wrangler-api-1       | goroutine 119 [running]:
receipt-wrangler-api-1       | net/http.(*conn).serve.func1()
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:1854 +0xbf
receipt-wrangler-proxy-1     | 192.168.0.199 - - [04/Dec/2023:14:56:26 +0000] "POST /api/receipt/quickScan HTTP/1.1" 502 559 "http://192.168.0.113:9082/receipts/group/2" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0" "-"
receipt-wrangler-api-1       | panic({0xd90d20, 0x155d730})
receipt-wrangler-api-1       |  /usr/local/go/src/runtime/panic.go:890 +0x263
receipt-wrangler-api-1       | github.com/sashabaranov/go-openai.(*Client).fullURL(0xd8a700?, {0xe73a50, 0x11}, {0xc000090e90, 0x1, 0x466a56?})
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/sashabaranov/go-openai@v1.16.0/client.go:209 +0x49
receipt-wrangler-api-1       | github.com/sashabaranov/go-openai.(*Client).CreateChatCompletion(_, {_, _}, {{0xe6fdae, 0xd}, {0xc0000c9240, 0x1, 0x1}, 0x0, 0x0, ...})
receipt-wrangler-proxy-1     | 2023/12/04 14:56:26 [error] 29#29: *37 upstream prematurely closed connection while reading response header from upstream, client: 192.168.0.199, server: , request: "POST /api/receipt/quickScan HTTP/1.1", upstream: "http://172.19.0.3:8081/api/receipt/quickScan", host: "192.168.0.113:9082", referrer: "http://192.168.0.113:9082/receipts/group/2"
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/sashabaranov/go-openai@v1.16.0/chat.go:165 +0x126
receipt-wrangler-api-1       | receipt-wrangler/api/internal/ai.(*AiClient).OpenAiChatCompletion(0xc000091298)
receipt-wrangler-api-1       |  /go/api/internal/ai/ai_client.go:98 +0x2bf
receipt-wrangler-api-1       | receipt-wrangler/api/internal/ai.(*AiClient).CreateChatCompletion(0x160e110?)
receipt-wrangler-api-1       |  /go/api/internal/ai/ai_client.go:36 +0x3b
receipt-wrangler-api-1       | receipt-wrangler/api/internal/services.ReadReceiptData({_, _})
receipt-wrangler-api-1       |  /go/api/internal/services/ai.go:63 +0x1f5
receipt-wrangler-api-1       | receipt-wrangler/api/internal/services.ReadReceiptImageFromFileOnly({_, _})
receipt-wrangler-api-1       |  /go/api/internal/services/receipt_image.go:78 +0xd8
receipt-wrangler-api-1       | receipt-wrangler/api/internal/services.MagicFillFromImage({{_, _, _}, {_, _}})
receipt-wrangler-api-1       |  /go/api/internal/services/receipt_image.go:99 +0x118
receipt-wrangler-api-1       | receipt-wrangler/api/internal/handlers.QuickScan.func1({0xfbc940, 0xc00016e7e0}, 0xe8a754?)
receipt-wrangler-api-1       |  /go/api/internal/handlers/receipts.go:212 +0x3c9
receipt-wrangler-api-1       | receipt-wrangler/api/internal/handlers.HandleRequest({{0xe8318e, 0x1c}, {0xfbc940, 0xc00016e7e0}, 0xc00022ab00, {0xe6836e, 0x6}, {0xe8a754, 0x1}, {0x0, ...}, ...})
receipt-wrangler-api-1       |  /go/api/internal/handlers/generic_handler.go:52 +0x422
receipt-wrangler-api-1       | receipt-wrangler/api/internal/handlers.QuickScan({0xfbc940, 0xc00016e7e0}, 0xc00022ab00)
receipt-wrangler-api-1       |  /go/api/internal/handlers/receipts.go:257 +0x218
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0xd8ab20?, {0xfbc940?, 0xc00016e7e0?}, 0xc000471640?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc0000805a0, {0xfbc940, 0xc00016e7e0}, 0xc00022ab00)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.10/mux.go:444 +0x236
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0xc00022aa00?, {0xfbc940?, 0xc00016e7e0?}, 0x160e110?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | github.com/auth0/go-jwt-middleware/v2.(*JWTMiddleware).CheckJWT.func1({0xfbc940, 0xc00016e7e0}, 0xc00022aa00)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/auth0/go-jwt-middleware/v2@v2.1.0/middleware.go:90 +0x248
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0x0?, {0xfbc940?, 0xc00016e7e0?}, 0xd?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | receipt-wrangler/api/internal/middleware.MoveJWTCookieToHeader.func1({0xfbc940, 0xc00016e7e0}, 0xc00022aa00)
receipt-wrangler-api-1       |  /go/api/internal/middleware/jwt.go:22 +0x195
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0xc00035e690?, {0xfbc940?, 0xc00016e7e0?}, 0xc0002e5920?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc0000805a0, {0xfbc940, 0xc00016e7e0}, 0xc00022aa00)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.10/mux.go:73 +0x355
receipt-wrangler-api-1       | github.com/go-chi/chi/v5.(*Mux).Mount.func1({0xfbc940, 0xc00016e7e0}, 0xc00022aa00)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.10/mux.go:316 +0x1c4
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0xd8ab20?, {0xfbc940?, 0xc00016e7e0?}, 0xc000134ed5?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | github.com/go-chi/chi/v5.(*Mux).routeHTTP(0xc000080360, {0xfbc940, 0xc00016e7e0}, 0xc00022aa00)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.10/mux.go:444 +0x236
receipt-wrangler-api-1       | net/http.HandlerFunc.ServeHTTP(0xe50b60?, {0xfbc940?, 0xc00016e7e0?}, 0x155d430?)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2122 +0x2f
receipt-wrangler-api-1       | github.com/go-chi/chi/v5.(*Mux).ServeHTTP(0xc000080360, {0xfbc940, 0xc00016e7e0}, 0xc00022a700)
receipt-wrangler-api-1       |  /go/pkg/mod/github.com/go-chi/chi/v5@v5.0.10/mux.go:90 +0x310
receipt-wrangler-api-1       | net/http.serverHandler.ServeHTTP({0xfba7f8?}, {0xfbc940, 0xc00016e7e0}, 0xc00022a700)
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:2936 +0x316
receipt-wrangler-api-1       | net/http.(*conn).serve(0xc0004f63f0, {0xfbd078, 0xc000491aa0})
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:1995 +0x612
receipt-wrangler-api-1       | created by net/http.(*Server).Serve
receipt-wrangler-api-1       |  /usr/local/go/src/net/http/server.go:3089 +0x5ed

This is my docker-compose.yml file:

version: "3.5"
services:
  api:
    image: noah231515/receipt-wrangler-api:latest
    restart: always
    environment:
      DB_FILENAME: wrangler.sqlite
      DB_ENGINE: sqlite
    working_dir: /go/api
    command: ./api --env prod
    ports:
      - 9080:8081
    volumes:
      - ./config:/go/api/config
      - ./data:/go/api/data
      - ./sqlite:/go/api/sqlite
      - ./logs:/go/api/logs
  proxy:
    image: noah231515/receipt-wrangler-proxy:latest
    ports:
      - 9082:80
    depends_on:
      - api
      - frontend

  frontend:
    image: noah231515/receipt-wrangler-desktop:latest
    restart: always
    ports:
      - 9081:80

Here my config.prod.json:

{
  "aiSettings": {
    "type": "openAi",
    "url": "url",
    "ApiHidden"
  },
  "secretKey": "Hidden"
  "emailPollingInterval": 1800, // 30 minutes
  "emailSettings": [
    {
      "host": "imap.gmail.com", // Your email provider's imap server
      "port": 993, // Your email provider's imap port
      "username": "Hidden", // Your email
      "password": "Hidden" // Your password or app password
    }
  ]
}

feature-config.prod.json

{
  "aiPoweredReceipts": true,
  "enableLocalSignUp": false
}

Best Regards Stefano

Noah231515 commented 11 months ago

@wes1993 Thanks for checking out Receipt Wrangler, and for all the debugging information.

It seems that the OpenAI client isn't being initialized when the app starts, which will happen if the a To verify this, there should be some entries in the logs that log what is being initialized when. For example:

image

Can you confirm what Initializing lines show up for you? You may restart the compose stack to see which statements show up, and then check the app logs (not the docker logs).

wes1993 commented 11 months ago

Dear @Noah231515, thanks a lot for your reply, i can confirm that seems OpenAI is not initialized..

App2023/12/05 08:16:22 main.go:33: Initializing app...
App2023/12/05 08:16:22 main.go:55: Initializing Imagick...
App2023/12/05 08:16:22 main.go:61: Initializing Tesseract...
App2023/12/05 08:16:22 main.go:97: Initialize completed
App2023/12/05 08:37:01 email.go:92: exit status 1
App2023/12/05 08:37:01 email.go:67: exit status 1
App2023/12/05 08:37:01 generic_handler.go:55: exit status 1
App2023/12/05 08:37:02 email.go:92: exit status 1
App2023/12/05 08:37:02 email.go:67: exit status 1
App2023/12/05 08:37:02 generic_handler.go:55: exit status 1

Just to be sure, for using the OpenAI i need a paid subscription or something else? (Now i have a free account without card registered)

There is a problem with emails... When i try to push new emails client go in error...

Best Regards Stefano

Noah231515 commented 11 months ago

@wes1993

To use OpenAI's API, you do not need a subscription. This uses OpenAI's pay as you go for their API. It currently uses GPT3.5Turbo and it is very cheap. Of all the testing, and actual usage of their API, I have spent less than $1USD. Just wanted to give some perspective of the cost.

Are your configs being mounted correctly to the server container? Your config directory should contain your feature-config.prod.json and config.prod.json

Try logging into your host server and running the following command while the containers are running: docker exec -it receipt-wrangler-api-1 cat /go/api/config/config.prod.json docker exec -it receipt-wrangler-api-1 cat /go/api/config/feature-config.prod.json

The output should be your two config files.

The relevant ways the OpenAPI Client is not initialized is if AiPoweredReceipts is false, or the type in AiSettings is set to something other than "openAi", or if configs are not mounted, then default values will be used which makes me think is what is happening.