Skip to content

[DI] Fix returning NULL on invalid ref for synthetic services #19847

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

Closed
wants to merge 1 commit into from

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 2.7
Bug fix? yes
Tests pass? yes
Fixed tickets #19689 #19840 #19825
License MIT
Doc PR -

@dmaicher
Copy link
Contributor

dmaicher commented Sep 4, 2016

Unfortunately this does not fix #19840 for me

@nicolas-grekas
Copy link
Member Author

@dmaicher the code you posted is broken: it misses the ContainerInterface::NULL_ON_INVALID_REFERENCE argument.

@dmaicher
Copy link
Contributor

dmaicher commented Sep 4, 2016

why exactly? I mean as long as I'm in the request scope it should be safe to retrieve the request service, right? I mean it worked with symfony 2.8.9 😊

So as far as I can see the problem is that here https://github.com/symfony/symfony/blob/2.8/src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php#L82 the request is set to null which then triggers synchronizeRequestService which in my case then tries to retrieve the service again with get('request').

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Sep 4, 2016

I checked a bit more to understand how null could end up as a service value in the container. The answer is on request-scope change: when leaving the main scope, ->set('request', null) is called, and this null value is finally unset only after synchronizeRequestService has been called.
See https://github.com/nicolas-grekas/symfony/blob/0db8822b104790cf1071f13e4ce37a717d19756e/src/Symfony/Component/DependencyInjection/Container.php#L197-L208

@dmaicher you should send a PR on https://github.com/craue/CraueFormFlowBundle/blob/2.1.9/Resources/config/form_flow.xml#L29 to add on-invalid="null"

@dmaicher
Copy link
Contributor

dmaicher commented Sep 4, 2016

But technically as long as I'm in request scope I will always be able to retrieve the request service. So adding on-invalid="null" should not be necessary imho as the service is marked with scope="request"?

Isn't this a BC break how the behaviour of the container changed for setting service instances to null will now trigger "re-instantiation"?

@nicolas-grekas
Copy link
Member Author

nicolas-grekas commented Sep 4, 2016

@fabpot @stof I'd be happy to have your input here:

  1. when not leaving scope should synthetic services always throw and ignore the ContainerInterface::NULL_ON_INVALID_REFERENCE flag as is the case now (latest 2.7 and before)?
  2. when leaving scope should synthetic services always return null and ignore the ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE flag as it was the case before 2.7.10?

In both cases, this is untested behavior. This PR adds a test for 1.

@nicolas-grekas
Copy link
Member Author

I guess we should revert yes. This issue is gone on 3.0 since scopes are gone also.

@nicolas-grekas nicolas-grekas deleted the di-fix-bc-break branch September 4, 2016 17:06
nicolas-grekas added a commit that referenced this pull request Sep 6, 2016
…" (nicolas-grekas)

This PR was merged into the 2.7 branch.

Discussion
----------

Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)"

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| Tests pass?   | yes
| Fixed tickets | #19689 #19840 #19825 #19857
| License       | MIT
| Doc PR        | -

This reverts commit c89f80a, reversing
changes made to 386e5e7.

See discussion in #19847

I'll try adding test cases soon that ensure that:

- [x] *when not leaving scope* synthetic services always throw and ignore the `ContainerInterface::NULL_ON_INVALID_REFERENCE` flag (on 3.x also)
- [x] *when leaving scope* synthetic services always return null and ignore the `ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE`  (until 2.8 since scopes are gone in 3.x)

Commits
-------

8cb28bf [DI] Add anti-regression test
ac742df Revert "minor #19689 [DI] Cleanup array_key_exists (ro0NL)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants