免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4639 | 回复: 0
打印 上一主题 下一主题

Mojo::UserAgent 内存漏洞问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-07-01 20:47 |只看该作者 |倒序浏览
本帖最后由 hbasrc 于 2018-07-01 21:19 编辑

先推荐一篇好文:circular-references-memory-leaks-and-mojo

找到此文的起因是在使用Mojo::UserAgent 过程中发现有内存泄漏问题。
如下例:
  1. use 5.010;
  2. use strict;

  3. use Data::Printer;
  4. use Mojo::UserAgent;
  5. use Mojo::IOLoop;

  6. my @urls = qw/127.0.0.1 127.0.0.1:3000 www.metacpan.org www.perl.org/;

  7. my $ua = new Mojo::UserAgent(max_redirects => 2);
  8. my $max_conn = 4;
  9. my $alive = 0;

  10. my $id = Mojo::IOLoop->recurring( 0.2 => sub {
  11.     return if $alive >= $max_conn or @urls < 1;

  12.     for (1..$max_conn-$alive) {
  13.         my $url = shift @urls;
  14.         say 'HTTP Get: ', $url;
  15.         $alive++;
  16.         
  17.         $ua->get($url => sub {
  18.             my (undef, $tx) = @_;
  19.             if ($tx->success) {
  20.                 say $tx->res->dom->at('title')->text;
  21.             } else {
  22.                 say $tx->error->{message}, ': ', $tx->error->{code};
  23.             }

  24.             --$alive;
  25.             push @urls, $url;
  26.         });
  27.     }
  28. });

  29. Mojo::IOLoop->start;

  30. 1;

复制代码

在perl 5.16中上例运行后会有缓慢而又轻微的内存泄漏,
上面推荐的这篇文章很好的分析了在大量使用闭包的代码中产生内存泄漏的原因及避免的办法。
上例中如果换到perl 5.18+中问题就消失了。

但在perl 5.22中下面的代码又出现了严重内存泄漏:
  1. # Perl 5.22.4 + Mojolicious v7.48
  2. ./perl -MMojo::UserAgent -E "say Mojo::UserAgent->new->get('www.qq.com')->res->dom->at('title')->text while 1"
复制代码
问题可能出在DOM解析部分的模块,因为暂时用不到DOM解析,没去追踪内在问题。

评分

参与人数 1信誉积分 +10 收起 理由
523066680 + 10 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP