【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题
问题描述
在Azure Function Portal上显示: Azure Functions runtime is unreachable,引起的结果是Function App目前不工作,但是此前一直都是正常工作的,且没有对Azure Function做过任何的改动,那它是为什么出现这样的问题呢?

问题分析
Azure Functions runtime is unreachable 的错误是 ”Azure Functions 运行时不可访问”,此问题的最常见原因是函数应用失去了对其存储帐户的访问权限。首先我们根据官方文档( 排查错误:“Azure Functions 运行时不可访问” )排查以下每一点:
存储帐户已被删除
存储帐户应用程序设置已被删除
存储帐户凭据无效
存储帐户不可访问
每日执行配额已满
应用受防火墙保护
注:多个Function App之间应尽量避免共享Storage Account,在创建Function App的时候,需要关联独立的Storage Account.

在排查外以上每一点后,如果依旧出现 “Azure Functions runtime is unreachable”的问题,那么此时就需要分析当前所运行的Function是否由异常,是否由出现CPU 100%, Memory 100%,以及线程数等情况。
在这次的问题中,在Azure Function的日志中,发现大量的如下两种异常:
| 异常一 |
Microsoft.Azure.ServiceBus.MessageLockLostException : The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. Reference:ae0230de-86a9-xxxx-fdd, TrackingId:eaeef_xxxxxxxxxxxxxxxxxxxx0_B17, SystemTracker:api-11:Topic:inmessage, Timestamp:2021-06-02T06:21:22 at async Microsoft.Azure.ServiceBus.Core.MessageReceiver.OnRenewLockAsync(String lockToken) ….. at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Azure.ServiceBus.MessageReceivePump.RenewMessageLockTask(Message message,CancellationToken renewLockCancellationToken) |
与Service Bus相关 |
| 异常二 |
Microsoft.Azure.Storage.StorageException : The lease ID specified did not match the lease ID for the blob. at async Microsoft.Azure.Storage.Core.Executor.Executor.ExecuteAsync[T](RESTCommand`1 cmd,IRetryPolicy policy,OperationContext operationContext,CancellationToken token) at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() …. at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at async Microsoft.Azure.WebJobs.Host.Timers.TaskSeriesTimer.RunAsync(CancellationToken cancellationToken) at C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Timers\TaskSeriesTimer.cs : 147 |
与Storage Account相关 |
在异常信息中,并没有明确的指明当前Azure Funciton运行不正常及Azure Functions runtime is unreachable有关的信息。但是Service Bus的 MessageLockLostException 异常值得重点分析,因为MessageLockLostException 异常意味着Function在消费Service Bus的消息的时候,由于处理消息的时间过长,超过了Meesage Lock(锁定,默认30秒),消息无法消费使得回滚会Service Bus的队列中,然后进行下一轮的消费,如此往返。最终表现就是Azure Funciton运行不正常。为了验证这一步,需要收集Function的DUMP文件。PS: 如何获取Windows下Function的DUMP,可参考博文:快速获取DUMP文件(App Service for Windows(.NET/.NET Core): https://www.cnblogs.com/lulight/p/13574331.html
检查内存DUMP,确认大量线程(基本上是所有线程)都在等待执行 调用HttpClient上传这一步。而线程池此时已耗尽,无法创建新的线程继续完成Http请求。导致Azure Function运行不正常,无法消费Service Bus中的消息,也引起了 Microsoft.Azure.ServiceBus.MessageLockLostException 异常

(收集DUMP后可使用Visual Studio 2019查看DUMP中的Stack信息)
综上,根据DUMP文件中的发现,找到了问题原因:应用程序代码在异步代码调用中使用了Wait方法,导致在Service Bus消息的高峰期间,进程池耗尽,无法正常运行并处理消息,也引起 Azure Functions runtime is unreachable,在最佳的操作要求中,有明确的要求:
“在 C# 中,请始终避免引用 Result 属性或在 Task 实例上调用 Wait 方法。 这种方法会导致线程耗尽。”

解决办法
一:从线程池耗尽的方面入手,增加ThreadPool。在FunctionApp上更改平台配置,改为按照64位模式运行。由于32位的程序(x86)的Function最大线程数位125,而64位修改后,ThreadPool最大值提升到32767。(如果应用打包时target的x86,则需要重新打包应用target x64)。
二:修改代码。移除在async方法中所调用的wait()方法,修改位await。PS: 使用异步代码时,里面应该一路使用async和await。
参考资料
排查错误:“Azure Functions 运行时不可访问”: https://docs.azure.cn/zh-cn/azure-functions/functions-recover-storage-account
提高 Azure Functions 的性能和可靠性的最佳做法: https://docs.azure.cn/zh-cn/azure-functions/functions-best-practices#avoid-sharing-storage-accounts
【完】
【Azure 应用服务】由 Azure Functions runtime is unreachable 的错误消息推导出 ASYNC(异步)和 SYNC(同步)混用而引起ThreadPool耗尽问题的更多相关文章
- 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题
一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...
- 【Azure 应用服务】Azure Function App使用SendGrid发送邮件遇见异常消息The operation was canceled,分析源码逐步最终源端
问题描述 在使用Azure Function App的SendGrid Binging功能,调用SendGrid服务器发送邮件功能时,有时候遇见间歇性,偶发性异常.在重新触发SendGrid部分的Fu ...
- 【Azure 应用服务】Azure Function 启用 Managed Identity后, Powershell Funciton出现 ERROR: ManagedIdentityCredential authentication failed
问题描述 编写Powershell Function,登录到China Azure并获取Azure AD User信息,但是发现遇见了 [Error] ERROR: ManagedIdentityCr ...
- 【Azure 应用服务】Azure App Service 自带 FTP服务
问题描述 Azure PaaS服务是否有FTP/S服务呢? 回答问题 应用服务(Web App/App Service)在创建时候,默认创建了FTP服务并自动开启,用于应用部署.但它不是适合作为FTP ...
- 【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
问题描述 在上篇博文"[Azure 应用服务]App Service for Linux 中实现 WebSocket 功能 (Python SocketIO)"中,实现了通过 HT ...
- 【Azure 应用服务】Azure Web App的服务(基于Windows 操作系统部署)在被安全漏洞扫描时发现了TCP timestamps漏洞
问题背景 什么是TCP timestamps(TCP 时间戳)? The remote host implements TCP Timestamps, as defined by RFC1323 (h ...
- 【Azure 应用服务】Azure Function App 执行PowerShell指令[Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt]错误
问题描述 使用PowerShell脚本执行获取Azure订阅列表的指令(Get-Azsubscription -TenantId $tenantID -DefaultProfile $cxt).在本地 ...
- 【Azure 应用服务】Azure Function HTTP 触发后, 230秒就超时。而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间?
问题描述 Azure Function HTTP 触发后, 230秒就超时,而其他方式触发的Function, 执行5分钟后也超时,如何调整超时时间? 问题分析 查阅官方文档,对函数应用超时持续时间有 ...
- 【Azure 应用服务】Azure Mobile App (NodeJS) 的服务端部署在App Service for Windows中出现404 Not Found -- The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
问题描述 使用NodeJS的后端应用,开发一个Mobile App的服务端,手机端通过REST API来访问获取后端数据.在本地编译好后,通过npm start启动项目,访问效果如下: 但是,当把项目 ...
随机推荐
- 分布式任务调度系统:xxl-job
任务调度,通俗来说实际上就是"定时任务",分布式任务调度系统,翻译一下就是"分布式环境下定时任务系统". xxl-job一个分布式任务调度平台,其核心设计目标是 ...
- top:0;bottom:0;left:0;right:0;同时使用的效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android NDK编程之Android.mk和Application.mk
Android编程使用NDK必须创建一个jni文件夹,并且jni文件里一般包含有C/C++的源码文件.Android..mk文件.Application.mk文件(可选),Android.mk文件的编 ...
- 【原创】Centos8安装ansible
1.安装步骤 # 安装epel扩展源 dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rp ...
- 路由器逆向分析------Running Debian MIPS Linux in QEMU
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/70176583 下面的文章内容主要参考英文博客<Running Debian ...
- UVA11992不错的线段树段更新
题意: 给你一个矩阵,最大20*50000的,然后有三个操作 1 x1 y1 x2 y2 v 把子矩阵的值全部都加上v 2 x1 y1 x2 y2 v 把子矩阵的值全部都变成v 2 x ...
- CVE-2013-1347:Microsoft IE CGenericElement UAF 漏洞利用样本分析
CVE-2013-1347 漏洞是典型的 IE 浏览器 UAF 漏洞,所以其利用方法和一般的 IE 浏览器漏洞的利用方法非常相似,所以流程大体上可以分为这些步骤:(1) 对象被释放 (2) 精确覆盖被 ...
- android 代码中使用textAppearance
一开始在代码中我以为使用tvAge.setTextAppearance(context, resid);这样的的方式就能行, 运行之后发现这个设置并未生效,于是到处搜索在代码中设置系统样式的的解决方法 ...
- CVPR2021| TimeSformer-视频理解的时空注意模型
前言: transformer在视频理解方向的应用主要有如下几种实现方式:Joint Space-Time Attention,Sparse Local Global Attention 和Axial ...
- 使用MindSpore的线性神经网络拟合非线性函数
技术背景 在前面的几篇博客中,我们分别介绍了MindSpore的CPU版本在Docker下的安装与配置方案.MindSpore的线性函数拟合以及MindSpore后来新推出的GPU版本的Docker编 ...