本文由簡體中文內容自動轉碼而成。阿里雲不保證此自動轉碼的準確性、完整性及時效性。本文内容請以簡體中文版本為準。

錯誤處理

更新時間:2024-08-04 20:02

本文介紹Custom Runtime運行環境常見的錯誤類型及排查方法。

執行個體啟動失敗(Failed to start function instance)

報錯樣本

The function cannot be started. Failed to start function instance. Error: the file /code/bootstrap is not exist

報錯排查

函數執行個體啟動失敗,一般是啟動命令異常或者啟動命令不存在。

  • 如果未設定啟動命令,Function Compute預設使用/code/bootstrap作為啟動命令。如果程式碼封裝中沒有該檔案,可增加/code/bootstrap指令碼,或修改啟動命令

  • 如果已設定啟動命令,請參考報錯資訊中的Error: the file xxx is not exist,確認該檔案是否存在。

關於設定啟動命令的具體操作,請參見建立函數

執行個體健全狀態檢查失敗(Function instance health check failed)

報錯樣本

Function instance health check failed on port 9001 in 120 seconds.\nLogs:

報錯排查

函數執行個體健全狀態檢查失敗,一般是代碼中監聽的IP地址或者連接埠設定錯誤導致。函數執行個體啟動後,平台會根據函數配置的連接埠進行4層連通性檢查,若在逾時時間內檢查不通過,則返回Function instance health check failed報錯。

監聽地址和連接埠必須滿足以下條件。

  • 監聽地址

    代碼中的監聽IP地址必須設定為0.0.0.0*,不能設定為127.0.0.0localhost

  • 監聽連接埠

    監聽連接埠必須和函數配置中的連接埠保持一致。Custom Runtime運行時預設監聽連接埠為9000

    • 如果使用預設連接埠,請確保代碼中HTTP Server監聽的連接埠也是9000

    • 如果設定了監聽連接埠,請確保代碼中HTTP Server監聽的連接埠與其一致。

關於設定監聽連接埠的具體操作,請參見建立函數

執行個體進程異常退出(Function instance exited unexpectedly)

報錯樣本

Function instance exited unexpectedly(code 2, message:no such file or directory) with start command '/code/bootstrap '.
Logs:
  • Function instance exited unexpectedly:表示執行個體啟動進程異常退出。

  • code 2, message:no such file or directory:表示執行個體啟動進程的Linux退出碼和該退出碼的含義。

  • with start command '/code/bootstrap ':表示該執行個體的啟動命令。

說明

進程退出碼及其含義僅作為問題排查的參考,不能完全確定執行個體退出原因,因為該退出碼可能是代碼中實現,與Linux退出碼含義不完全相匹配。

報錯排查

  • 啟動命令沒有可執行許可權

    The function cannot be started. Function instance exited unexpectedly(code 13, message:permission denied) with start command '/code/bootstrap '.

    如果執行個體啟動命令沒有可執行許可權,報錯資訊中的退出碼一般為code 13, message:permission denied。可以在打包代碼前,執行chmod 755 bootstrapchmod 777 bootstrapchmod +x bootstrap賦予檔案的可執行許可權。

  • 檔案不存在

    Function instance exited unexpectedly(code 2, message:no such file or directory) with start command 'python3 not_exist_file.py '.
    Logs:xxx

    如果啟動參數中的檔案不存在,則報錯資訊中的退出碼一般為code 2, message:no such file or directory。特殊情況下,報錯資訊中的退出碼可能不是code 2, message:no such file or directory或無退出碼,此時,需要根據報錯日誌進行排查。

    下文列舉不同啟動命令下,檔案不存在的報錯資訊。

    • Python

      當執行個體啟動命令為python3 not_exist_file.py時,退出碼為code 2, message:no such file or directory

      如果將該啟動命令放到Shell指令碼中,樣本如下,然後修改啟動命令為/code/bootstrap。執行函數會出現報錯/code/not_exist_file.py不存在。

      Shell指令碼樣本

      #!/bin/bash
      python3 not_exist_file.py

      報錯資訊

      Function instance exited unexpectedly(code 2, message:no such file or directory) with start command '/code/bootstrap '.
      Logs:python3: can't open file '/code/not_exist_file.py': [Errno 2] No such file or directory
    • Node.js

      當執行個體啟動命令為npm run start,在package.json中配置的指令碼啟動命令為node ./not_exist_file.js,報錯資訊如下。

      Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'npm run start '.
      Logs:> nodejs-express@0.0.0 start /code
      > node ./not_exist_file.js
      
      internal/modules/cjs/loader.js:905
        throw err;
        ^
      
      Error: Cannot find module '/code/not_exist_file.js'
          at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
          at Function.Module._load (internal/modules/cjs/loader.js:746:27)
          at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
          at internal/main/run_main_module.js:17:47 {
        code: 'MODULE_NOT_FOUND',
        requireStack: []
      }
      npm ERR! code ELIFECYCLE
      npm ERR! errno 1
       ERR! nodejs-express@0.0.0 start: `node ./not_exist_file.js`
       npm ERR!
       ERR! Failed at the nodejs-express@0.0.0 start script.
      npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
      
      npm ERR! A complete log of this run can be found in:
      npm ERR!     /root/.npm/_logs/2022-10-31T08_02_29_434Z-debug.log

      從報錯日誌中可以找到原因Error: Cannot find module '/code/not_exist_file.js',即/code/not_exist_file.js不存在。報錯的退出碼為code 1, message:operation not permitted

      當執行個體啟動命令為node ./not_exist_file.js時,報錯類似。

    • Java

      當執行個體啟動命令為java -Dserver.port=9000 -jar target/not_exist_file.jar時,報錯資訊如下。

      Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'java -Dserver.port=9000 -jar target/not_exist_file.jar '.
      Logs:Error: Unable to access jarfile target/not_exist_file.jar

      從報錯日誌中可以找到原因Unable to access jarfile target/not_exist_file.jar,即target/not_exist_file.jar檔案不存在。報錯的退出碼為code 1, message:operation not permitted

    • PHP

      當執行個體啟動命令為php not_exist_file.php時,報錯資訊如下。

      Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'php not_exist_file.php '.
      Logs:Could not open input file: not_exist_file.php

      從報錯日誌中可以找到原因Could not open input file: not_exist_file.php,即not_exist_file.php檔案不存在。報錯的退出碼為code 1, message:operation not permitted

    • .NET Core

      當執行個體啟動命令為dotnet ./target/NotExistFile.dll時,報錯資訊如下。

      Function instance exited unexpectedly(code 145) with start command 'dotnet ./target/NotExistFile.dll '.
      Logs:Could not execute because the application was not found or a compatible .NET SDK is not installed.
      Possible reasons for this include:
        * You intended to execute a .NET program:
            The application './target/NotExistFile.dll' does not exist.
        * You intended to execute a .NET SDK command:
            It was not possible to find any installed .NET SDKs.
            Install a .NET SDK from:
              https://aka.ms/dotnet-download

      其報錯日誌中有詳細的排查方法,./target/NotExistFile.dll檔案不存在或.NET SDK沒有安裝。報錯的退出碼為code 145

    • Ruby

      當執行個體啟動命令為ruby not_exist_file.rb時,報錯資訊如下。

      Function instance exited unexpectedly(code 1, message:operation not permitted) with start command 'ruby not_exist_file.rb '.
      Logs:Traceback (most recent call last):
      ruby: No such file or directory -- not_exist_file.rb (LoadError)

      從報錯日誌中可以找到原因No such file or directory -- not_exist_file.rb,即not_exist_file.rb檔案不存在。報錯的退出碼為code 1, message:operation not permitted

  • 檔案格式錯誤

    Function instance exited unexpectedly(code 8, message:exec format error) with start command '/code/bootstrap '.
    Logs:

    Custom Runtime運行時環境為x86-64架構的Linux,需要保證開機檔案相容該系統內容。如果啟動命令為Shell指令碼,需要確保檔案為Linux格式,並且檔案包含Shell的解釋行#!。如果啟動命令為二進位可執行檔,需要確保該檔案為相容Linux系統的ELF檔案格式。具體資訊如下。

    • 啟動命令Shell指令碼解釋行錯誤

      當Shell指令碼缺少首行解釋行,或者解釋行錯誤時,執行個體退出碼一般為8 exec format error。因此,需要在檔案首行添加正確的解釋行。

      如您需要使用Bash運行該指令碼,可以在檔案首行加上命令#!/usr/bin/env bash#!/bin/bash,推薦使用#!/usr/bin/env bash。在Custom Runtime系統內容中,/bin/sh預設為/bin/bash,因此,也可以使用命令#!/usr/bin/env sh#!/bin/sh

    • 啟動命令Shell指令碼為Windows格式

      執行以下測試指令碼。

      #!/usr/bin/env bash
      node /code/index.js

      報錯如下。

      Function instance exited unexpectedly(code 127, message:key has expired) with start command '/code/bootstrap '.
      Logs:/usr/bin/env: ‘bash\r’: No such file or directory

      在錯誤記錄檔中bash\r表示在bash後面多餘一個字元\r。Unix檔案分行符號為\n,Windows檔案分行符號為\r\n,因此,該檔案是Windows格式。

      如果您的指令碼在Windows系統下建立,需要將指令碼格式轉換為Unix格式。您可以在Linux系統下通過執行命令dos2unix進行轉換,或者使用Function Compute的WebIDE進行轉換。更多資訊,請參見如何使用Function Compute的Web IDE轉換檔格式?

    • 啟動命令為二進位可執行檔

      如果啟動命令為可執行檔,請確保該檔案為相容Linux系統的ELF檔案格式。例如,在一台M1晶片的Mac機器上,使用預設配置GOOS=darwin GOARCH=arm64編譯Golang代碼,然後進行打包上傳並測試,報錯資訊如下所示。

      Function instance exited unexpectedly(code 8, message:exec format error) with start command './main '.
      Logs:

      執行個體退出碼為8 exec format error,表示檔案格式錯誤。需要在編譯時間添加配置GOOS=linux GOARCH=amd64。具體資訊,請參見編譯部署程式碼封裝

      展開查看如何判斷可執行檔的格式。

      • 查看使用GOOS=darwin GOARCH=arm64編譯的可執行檔格式,如下所示。

        $ file main
        main: Mach-O 64-bit arm64 executable, flags:<|DYLDLINK|PIE>

        其檔案格式為Mach-O,CPU架構為arm64,與Custom Runtime運行時底層環境不相容。

      • 查看使用GOOS=linux GOARCH=amd64編譯的可執行檔格式,如下所示。

        $ file main
        main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, Go BuildID=xxx, not stripped

        其檔案格式為ELF,CPU架構為x86-64,與Custom Runtime運行時底層環境相容。

      說明
      • Linux系統的可執行檔格式一般為ELF格式。

      • Mac系統的可執行檔是Mach-O格式。

      • Windows系統的可執行檔是PE格式。

  • 常見退出碼

    除了以上列舉的退出碼以外,還有其他的常見錯誤情況,具體資訊如下。

    • Exit Code 137

      程式收到訊號SIGKILL異常退出,一般情況是OOMKilled(Out of Memory)問題,程式因記憶體不足而退出。此時,可以嘗試調大函數的記憶體規格。

更多資訊

如果遇到的錯誤類型未包含在本文列舉的錯誤清單中,請參見Custom Runtime FAQ進一步排查原因。

  • 本頁導讀 (1, M)
  • 執行個體啟動失敗(Failed to start function instance)
  • 報錯樣本
  • 報錯排查
  • 執行個體健全狀態檢查失敗(Function instance health check failed)
  • 報錯樣本
  • 報錯排查
  • 執行個體進程異常退出(Function instance exited unexpectedly)
  • 報錯樣本
  • 報錯排查
  • 更多資訊
文檔反饋