Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yaml implicit resolver leak #67

Closed
xqliang opened this issue May 31, 2017 · 2 comments
Closed

yaml implicit resolver leak #67

xqliang opened this issue May 31, 2017 · 2 comments
Labels

Comments

@xqliang
Copy link

xqliang commented May 31, 2017

Every time when we call yaml.dump, it will call the resolve method:

    def resolve(self, kind, value, implicit):
        if kind is ScalarNode and implicit[0]:
            if value == u'':
                resolvers = self.yaml_implicit_resolvers.get(u'', [])
            else:
                resolvers = self.yaml_implicit_resolvers.get(value[0], [])
            resolvers += self.yaml_implicit_resolvers.get(None, [])

As show above, the last line resolvers += self.yaml_implicit_resolvers.get(None, []) in resolve will change the yaml_implicit_resolvers inplace by accident, making yaml_implicit_resolvers size become more and more bigger, the yaml.dump will slow down, and finally use up 100% CPU.

reproduce:

import re

import yaml


def test_implicit_resolver_leak():
    # Add any custom resolver
    tag, regexp = '!any_resolver', re.compile('AnyResolver')
    yaml.add_implicit_resolver(tag, regexp)

    none_resolvers = yaml.Dumper.yaml_implicit_resolvers.get(None, [])
    assert (tag, regexp) in none_resolvers

    old_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
    assert (tag, regexp) not in old_f_resolvers

    # Dump at least one ScalarNode
    yaml.dump(False)

    new_f_resolvers = yaml.Dumper.yaml_implicit_resolvers.get('f', [])
    assert (tag, regexp) in new_f_resolvers  # leak here
@xqliang xqliang changed the title yaml.dump increase implicit resolver by accident, and finally use up 100% CPU yaml implicit resolver leak Aug 8, 2017
xqliang pushed a commit to xqliang/pyyaml that referenced this issue Aug 8, 2017
bridadan pushed a commit to bridadan/pyyaml that referenced this issue Aug 17, 2017
@perlpunk
Copy link
Member

Thanks! I can reproduce it here.

@perlpunk
Copy link
Member

This was fixed by #441 which was released in 5.4.
This should have been in
https://github.com/yaml/pyyaml/projects/5
I guess.

@ingydotnet I close this for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants