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

--auto-gen-config runs forever #9946

Closed
jaredbeck opened this issue Jul 21, 2021 · 7 comments
Closed

--auto-gen-config runs forever #9946

jaredbeck opened this issue Jul 21, 2021 · 7 comments
Assignees

Comments

@jaredbeck
Copy link
Contributor

Expected behavior

--auto-gen-config should finish eventually.

Actual behavior

--auto-gen-config ran at 100% CPU for 25 minutes before I gave up.

top -pid 27995
PID    COMMAND      %CPU  TIME     #TH  #WQ  #POR MEM  PURG CMPR PGRP  PPID  STATE   BOOSTS    %CPU_ME %CPU_OTHRS UID  FAULT COW  MSGSEN MSGR SYSBSD SYSM CSW
27995  ruby         99.8  24:03.73 2/1  0    22   103M 0B   0B   27995 93817 running *0[1]     0.00000 0.00000    redacted  40598 717  13972  85   217280 1309 264006+

With --debug, the last line printed before it hangs is:

Scanning /Users/jaredbeck/redacted/lib/tasks/redacted.rake

Workaround

  1. Deleted the .rake file
  2. Ran --auto-gen-config
  3. Restored the .rake file

Why can't --auto-gen-config handle this .rake file?

I don't know. --debug doesn't provide any clues, so I can only speculate. The file (below, redacted) has a very long string. Perhaps that's the culprit?

desc 'Redacted'
task :redacted => :environment do
  Dir.glob("#{Rails.root}/app/redacted.rb").each { |file| require file }
  redacted1 = 'redacted
300 line
28 kilobyte
string'
  # redacted 338 char long comment
  label = OpenStruct.new(redacted2: redacted1)
  redacted6 = Redacted.new(redacted3: 999).generate_pssl(redacted4: true, redacted5: label)
  ff = File.open("redacted.pdf", "w")
  ff.write(redacted6.force_encoding('utf-8'))
  ff.close
end

Steps to reproduce the problem

Try creating a file with a 28 kilobyte string?

RuboCop version

bundle exec rubocop -V
1.18.3 (using Parser 3.0.2.0, rubocop-ast 1.8.0, running on ruby 2.6.3 x86_64-darwin20)
@jonas054
Copy link
Collaborator

I have not been able to reproduce. @jaredbeck Do you also get it if there's no .rubocop.yml file? I'm thinking that it's perhaps related to your configuration somehow.

@jaredbeck
Copy link
Contributor Author

jaredbeck commented Jul 23, 2021

Hi Jonas, Please try the following minimal reproduction:

mkdir issue_9946 && cd issue_9946
ruby -v # 2.7.2
rubocop -v # 1.18.3
curl -O https://gist.githubusercontent.com/jaredbeck/cb1b6e1b826d03a30184e539a84bbae4/raw/9103bae827a4756510e190227fa48e788c49bffe/rubocop_issue_9946.rb
rm .rubocop*
rubocop --auto-gen-config --debug

It should print

...
Phase 2 of 2: run all cops
...
Scanning .../rubocop_issue_9946.rb

And then run forever, at 100% CPU.

Critical problematic input

A long comment (in this case, 13228 chars), of random content.

Suggestions

Add a Timeout? If --auto-gen-config times out on a specific file, add it to AllCops: Exclude:?

@dvandersluis
Copy link
Member

Do you have the same behaviour if you just run rubocop (without --auto-gen-config) on that file? My guess is that one of the cops is not completing due to the super long string (maybe LineLength?)

@jaredbeck
Copy link
Contributor Author

Do you have the same behaviour if you just run rubocop (without --auto-gen-config) on that file?

Yes.

rm .rubocop*
rubocop --debug # 1.18.4
For /.../redacted: Default configuration from /redacted/gems/rubocop-1.18.4/config/default.yml
Inspecting 1 file
Scanning /.../redacted/redacted.rb

Also, the ruby version doesn't seem to matter. I reproduced this with both ruby 2.7.2 and 3.0.2.

@jonas054
Copy link
Collaborator

I've been able to reproduce the problem. Thanks @jaredbeck! And I've found that it's the Style/CommentedKeyword cop that hangs. It's actually possible that it will finish at some point, but the time is spent in regexp matching, trying to match /(?<keyword>\S+).*#/ against the very long comment in this example.

It's a regular expression that can't always be matched in reasonable time. I suggest we correct this cop rather than monitoring time-outs in cops.

@jonas054 jonas054 self-assigned this Jul 23, 2021
@jonas054
Copy link
Collaborator

I'll try to fix it.

@koic koic closed this as completed in 3824661 Jul 23, 2021
koic added a commit that referenced this issue Jul 23, 2021
[Fix #9946] Avoid slow regexp match in Style/CommentedKeyword
@jaredbeck
Copy link
Contributor Author

Thanks for the quick fix, everyone.

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

No branches or pull requests

3 participants