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

chef_version metadata violation raises incorrect error #9038

Closed
kimbernator opened this issue Oct 31, 2019 · 1 comment · Fixed by #9083

Comments

@kimbernator
Copy link

@kimbernator kimbernator commented Oct 31, 2019

Description

If a run-list includes a cookbook with a chef_version constraint that is not permissive of the version of chef infra client running, the error raised is incorrect due to an error in the code to finalize the run with an end message:

Running handlers:
[2019-10-31T13:22:15-05:00] ERROR: Running exception handlers
Running handlers complete
[2019-10-31T13:22:15-05:00] ERROR: Exception handlers complete
Chef Infra Client failed. 0 resources updated in 10 seconds
[2019-10-31T13:22:15-05:00] FATAL: NoMethodError: undefined method `filtered_collection' for nil:NilClass

Running chef-client -l trace reveals an attempt at re-raising the correct error, which is:

[2019-10-31T13:22:15-05:00] TRACE: Re-raising exception: Chef::Exceptions::CookbookChefVersionMismatch - Cookbook '<cookbook_name>' version '0.5.0' depends on chef version [">= 12.14", "< 15"], but the running chef version is 15.4.45

In these lines:

chef/lib/chef/client.rb

Lines 303 to 317 in b499e05

logger.trace("Re-raising exception: #{run_error.class} - #{run_error.message}\n#{run_error.backtrace.join("\n ")}")
# If we failed really early, we may not have a run_status yet. Too early for these to be of much use.
if run_status
run_status.stop_clock
run_status.exception = run_error
run_failed
end
events.run_failed(run_error, run_status)
Chef::Application.debug_stacktrace(run_error)
raise run_error
ensure
Chef::RequestID.instance.reset_request_id
@run_status = nil
runlock.release
end

The exception is re-raised in a trace log, but line 308's run_failed call experiences its own error which is unhandled and results in all code until ensure (lines 310-312) not being executed and the CookbookChefVersionMismatch not being raised.

The unhandled error occurs when formatting the run ending message as a result of the updated_resources method in chef/resource_reporter.rb:

# get only the top level resources and strip out the subcollections
def updated_resources
@updated_resources ||= action_collection.filtered_collection(max_nesting: 0, up_to_date: false, skipped: false, unprocessed: false)
end

Changing line 186 to the following results in a chef-client failure that includes the more useful CookbookChefVersionMismatch error:

@updated_resources ||= action_collection&.filtered_collection(max_nesting: 0, up_to_date: false, skipped: false, unprocessed: false) || {}

(Changes being the & after action_collection to return nil instead of throwing a NoMethodError, and || {} at the end to assign {} to @updated_resources)

I am hesitant to submit a PR with this change since it feels like a cheap hack to resolve one specific error without addressing the fact that @action_collection is not being defined early enough for this error to be properly raised.

Chef Version

15.4.45

Platform Version

RHEL 6 and Windows Server 2008R2

Replication Case

Include a cookbook with chef_version constraints in metadata.rb that exclude the chef-client version running. Make sure cookbook synchronization is not disabled. No policy files.

Client Output

Included in description

Stacktrace

N/A

@lamont-granquist

This comment has been minimized.

Copy link
Contributor

@lamont-granquist lamont-granquist commented Nov 1, 2019

thats the correct fix, can you submit the PR?

lamont-granquist added a commit that referenced this issue Nov 12, 2019
closes #9043
closes #9038

Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.