Repository navigation
DLLs required by path find the DLLs they reference, loaded or alongside (rebased) - #63
Merged
Merged
Conversation
require loads a DLL by its path with Assembly.LoadFile, which on .NET Core
gives each file a load context of its own, where a reference by name finds
the application's assemblies and no other. A reference from one such DLL to
another fell to IronRuby's AssemblyResolve handler, which only searched
$LOAD_PATH. So two DLLs required by their paths, the second built against
the first, broke as soon as anything read the second's signatures:
require '/x/Demo.dll'
using_clr_extensions Demo
require '/x/Demo2.dll' # LoadError: Could not load file or assembly
# 'Demo, Version=1.0.0.0, ...'
Active extension methods are read as each assembly loads, so there the
require raised. Without them, the first call of a Demo2 method taking a Demo
type raised Errno::ENOENT instead. And when $LOAD_PATH held another file of
Demo, the handler loaded it as a second Demo, whose Widget isn't the one
Ruby made: Demo2's methods wouldn't take Ruby's Widgets.
The handler now tries three places in turn:
- The assemblies the runtime has loaded. One answers if it has the simple
name and culture asked for, at least the version asked for (the rule .NET's
binder applies to an application's assemblies), and the public key token if
the reference names one.
- $LOAD_PATH, as before, with its name-only check.
- The directory of the DLL that asked, if the runtime loaded it from a file,
roughly what .NET Framework's LoadFrom context did. A file there is loaded
only if its identity will do, and becomes the runtime's as a $LOAD_PATH
find does.
AssemblyResolve is the process's event, and every runtime in a process is
asked, in the order they hooked it. A runtime now leaves a reference made by
a DLL that another runtime loaded from a file to that runtime. Before, the
first runtime whose $LOAD_PATH held a copy of the dependency answered for
all of them, with its copy. IronRuby.Tests, which runs each test in a new
runtime of one process, is such a process: without this, the new tests
answer each other's references.
Require_PathLoadedDependency1-6 cover the reported case, the directory,
copies on $LOAD_PATH and next to the DLL that must not be loaded again,
versions, $LOAD_PATH before the directory, and a second runtime with copies
of its own. The fixture DLLs are written at test time with
System.Reflection.Metadata; PersistedAssemblyBuilder would need .NET 9.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le next to it is no answer $LOAD_PATH was the one place ResolveAssembly still matched by simple name only, and it loaded the file before checking even that. It came before the DLL's own directory, so an older Dep.dll on $LOAD_PATH answered a reference to a newer Dep sitting next to the DLL, which then failed later with a MissingMethodException. It now checks the file's identity with Satisfies before loading it, as the other two places do. Next to the DLL, every failure turned into a LoadError thrown out of the AssemblyResolve handler, which failed the type load of the DLL that asked: - A file there that isn't an assembly (a native DLL of the same name) or can't be read now leaves the reference unresolved, as if nothing were there. - A dependency that loads but whose extension methods can't be read (they name a type of a missing assembly; read as it loads once any `using' of a CLR namespace has run) is reported with a warning and still answers the reference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 59 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#53 rebased onto master, which now has #50 and #56. The original PR came from @sholden's branch, which still sits on the pre-rebase #50 and conflicts with master. Commits, authorship kept:
The last commit is a review fix on top: the
$LOAD_PATHlookup now checks the version (an older copy there no longer answers a reference to a newer one next to the DLL), and a bad file next to the DLL no longer fails the type load.Checked together with #55, all of them merged onto master 62655f8 (Debug, net8.0, Linux):
regen-initializers.shleavesInitializers.Generated.csunchanged; 0 build warnings; IronRuby.Tests 0 failures;spec/language2934 examples, 0 failures;spec/coreexception, mutex, conditionvariable, refinement,Module#refine/#using,main.usingandKernel#requireall 0 failures; the 101 LINQ samples print exactly101samples.expected;Util/error-message-matrix.rb,refinement-binding-matrix.rb,jit-matrix.rbandcondvar-matrix.rbmatch CRuby 4.0.6 line for line, andrefinement-matrix.rbdiffers only in object addresses.The description below is from #53 (sholden's).
With CLR extension methods active, a DLL that references another DLL failed to load when both were required by absolute path.
Requiring the same two DLLs through
$LOAD_PATHworked. master has the bug too, throughusing_clr_extensions.Extension methods only made it show up at the
require. Once any are active, IronRuby reads each new assembly's extension methods as it loads, and readingShout(this Widget)needsWidget. Without them therequiresucceeded, and the first call of a Demo2 method taking a Demo type raisedErrno::ENOENTfor the same missing assembly.Cause
requireloads a DLL by its path withAssembly.LoadFile, through the DLR'sPlatformAdaptationLayer.LoadAssemblyFromPath. On .NET Core,LoadFilegives each file a load context of its own. A reference by name from that context finds the application's own assemblies and nothing else, so Demo2's reference toDemofailed .NET's probing and went to IronRuby'sAppDomain.AssemblyResolvehandler. That handler,Loader.ResolveAssembly, searched$LOAD_PATHand nothing else.$LOAD_PATHhad a second problem. If it held a different file of Demo from the onerequirehad loaded, the handler loaded that file as a second Demo with its ownWidget. A Widget made before thatrequirethen got NoMethodError forshout, and passing it toDemo::LaterExtensions.Shoutraised TypeError.What changed
Loader.ResolveAssemblynow tries three places in turn.$LOAD_PATH, unchanged. It keeps its old check,AssemblyName.ReferenceMatchesDefinition, which on .NET compares simple names only.AssemblyName.GetAssemblyNameand loads it only if it satisfies the reference. Like a$LOAD_PATHfind, it's registered with the runtime, so its namespaces show up in Ruby.The order keeps a dependency from being loaded twice. Every
LoadFileof a different path makes a separate assembly with its own types. Because loaded assemblies come first, a copy on$LOAD_PATHor next to Demo2 is never loaded once Demo is.Why both
I kept both approaches, because each fails on its own.
With only the loaded-assembly step,
require '/x/Demo2.dll'still fails when Demo.dll sits next to it but was never required.$LOAD_PATH << '/x'; require 'Demo2.dll'already works in that case, and that's how a build's output folder is laid out.With only the directory step, a Demo loaded from somewhere else gets a second copy. If Demo comes from
/aand Demo2 from/b, which has its own Demo.dll, the probe loads/b/Demo.dll, and Demo2'sShouttakes that copy'sWidget, not the one Ruby made.The directory step runs last, so it can't change what
$LOAD_PATHresolves, and it only looks next to DLLs this runtime loaded from files. The handler .NET installs forAssembly.LoadFromhas the same limit. It only probes next to assemblies thatLoadFromloaded.Several runtimes in one process
AssemblyResolveis a process-wide event that every runtime hooks, and the CLR asks the handlers in the order they were added. Before this PR, the first runtime whose$LOAD_PATHheld a copy of a dependency answered for every runtime, with its own copy. The loaded-assembly step would have done the same with whatever copy the first runtime had loaded.Each runtime now records the DLLs it loads from files, and a process-wide
ConditionalWeakTablerecords those of every runtime. In all three steps, a runtime leaves a reference made by a DLL that another runtime loaded to that runtime. References from anything no runtime loaded from a file, such as IronRuby's own assemblies or a by-name load, are answered as before, so a program with one runtime sees no difference.IronRuby.Tests runs every test case in a new runtime of one process, so it hits this. I found it when
Require_PathLoadedDependency6failed. The runtime left over from test 5 still had that test's fixture directory on its$LOAD_PATH, and it answered test 6's reference.Behaviour changes
$LOAD_PATHholds another file of it. Before, that file was loaded as a second assembly. With one runtime, that's the only change to$LOAD_PATHresolution.require '/x/Demo.dll',load_assembly 'Demo'used to raiseAssembly 'Demo' not foundunless Demo was on$LOAD_PATH. Now it returns the loaded Demo, and so dorequire 'Demo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'andSystem::Reflection::Assembly.load('Demo').$LOAD_PATH.Tests
The tests write their fixture DLLs at test time with
System.Reflection.Metadata'sMetadataBuilder.PersistedAssemblyBuilderwould have been shorter, but it needs .NET 9 and CI also runs IronRuby.Tests on net8.0.PathLoadedWidgets.dllhas a classPathLoadedFixture.Widget. Each of the other DLLs is built against a given version of it and has an extension methodShout(this Widget).The tests turn extension methods on with
Context.ActivateExtensions, the callusing_clr_extensionsmakes.usingof a namespace doesn't exist on master, andusing_clr_extensionsprints #50's deprecation warning here but not on master, so either would have tied the commit to one branch.Require_PathLoadedDependency1is the reported case. Both DLLs are in one directory and required by path, with extension methods active.Require_PathLoadedDependency2requires only the extensions DLL.PathLoadedWidgetsis found next to it and registered.Require_PathLoadedDependency3loads Widgets froma/and makes a Widget, then requires the extensions fromb/.b/has its own copy of Widgets, andc/, on$LOAD_PATH, has a third. The Widget getsshout, and the process holds onePathLoadedWidgetsfrom the test's directory.Require_PathLoadedDependency4loads Widgets 2.0. Extensions built against 1.0 load and work, and extensions built against 3.0 raise LoadError.Require_PathLoadedDependency5checks that$LOAD_PATHstill resolves the reference, ahead of a copy next to the DLL.Require_PathLoadedDependency6runs a second runtime with its own copies. The test runtime first has a copy on its$LOAD_PATH, then one loaded.To check that the tests catch what they're meant to, I ran them against master's
Loader.csand against six broken versions of the fix.Loader.cs$LOAD_PATH.$LOAD_PATHbefore loaded assembliesResults below are from a Debug build on Linux with net10.0, unless noted.
DOTNET_ROLL_FORWARD=Major, and they pass. CI ran net8.0 for real (below).spec/languagepasses with 2935 examples and 0 failures.Kernel#require,require_relative,loadandautoloadspecs give 213 examples, 0 failures and 2 errors. Both errors are "with an unreadable file raises a LoadError", which getsErrno::EACCES. They happen with master'sLoader.cstoo.:loaded. later.rb, and a copy of it that requires both DLLs by absolute path, print the same three lines.Demo::LaterExtensions.Shout(widget)call after req.rb works. The by-name loads above return the path-loaded Demo. A Demo that's only on$LOAD_PATHresolves from there, and one that's nowhere still raises.Build/dlr-patches, 0102 included. IronRuby.Tests passes with 0 failures, and req.rb, req2.rb and req4.rb print:loaded. I removed the worktree afterwards.CI ran on this PR and passed all seven checks. IronRuby.Tests reported 0 failure lines on ubuntu and windows with both net8.0 and net10.0, and
spec/languagepassed with 2935 examples and 0 failures on net8.0 and net10.0. An earlier version of this description said CI doesn't run on the fork's PRs. It does.Not changed
The DLR's
LoadAssemblyFromPathstill callsAssembly.LoadFile. Loading each runtime's DLLs into one shared load context would let .NET resolve these references itself. But that changes how every DLL loads, the DLR is shared code, and a host can install its own platform layer, so a fix there wouldn't reach every host. The resolve handler is IronRuby's and works with any platform layer.Found, not fixed
requirethat fails while IronRuby reads the DLL's extension methods leaves the DLL registered with the runtime but out of$LOADED_FEATURES. Requiring it again returnstruebut never reads its extension methods, because the DLR'sScriptDomainManager.LoadAssemblyraisesAssemblyLoadedonce per assembly. After a failedrequire '/x/Demo2.dll', arequireof Demo and a retry,w.shoutis still a NoMethodError whileDemo::LaterExtensions.Shout(w)works. With this PR the firstrequirefails only when nothing satisfies the reference. master behaves the same. A DLL whose extension methods couldn't be read can be required again #54 fixes this, along with a worse variant: an unreadable DLL that was only queued made the nextusingraise and lost the extension methods of everything queued after it.requireof a second file with the same identity still loads a second copy, as inrequire '/a/Demo.dll'followed byrequire '/b/Demo.dll'. The resolver no longer makes copies on its own, butrequireloads the file it's given.Stack
using_clr_extensionsusing System::Linq: CLR extension methods scoped like C#'s using directive #50 scopes CLR extension methods withusingusing System::Linq: CLR extension methods scoped like C#'s using directive #50. It's the DLL loading fix thatusing System::Linq: CLR extension methods scoped like C#'s using directive #50's description mentions. The commit doesn't depend on Refinements: only refined calls pay, and the JIT and OSR keep working #49 orusing System::Linq: CLR extension methods scoped like C#'s using directive #50 and also applies to master on its own.🤖 Generated with Claude Code