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

Limit exposure to ConcurrentModificationException when sys props are replaced or mutated #22180

Merged
merged 2 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions compiler/src/dotty/tools/dotc/config/PathResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ object PathResolver {
/** Values found solely by inspecting environment or property variables.
*/
object Environment {
private def searchForBootClasspath = (
systemProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse ""
)
private def searchForBootClasspath = {
import scala.jdk.CollectionConverters.*
val props = System.getProperties
// This formulation should be immune to ConcurrentModificationExceptions when system properties
// we're unlucky enough to witness a partially published result of System.setProperty or direct
// mutation of the System property map. stringPropertyNames internally uses the Enumeration interface,
// rather than Iterator, and this disables the fail-fast ConcurrentModificationException.
val propNames = props.stringPropertyNames()
propNames.asScala collectFirst { case k if k endsWith ".boot.class.path" => props.getProperty(k) } getOrElse ""
}
Comment on lines +39 to +48
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to understand why this fallback exists, but I landed here and that was a dead end. Maybe we could just remove this code and do

def javaBootClassPath: String = propOrElse("sun.boot.class.path", "")

Copy link
Member

@SethTisue SethTisue Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably? for now let's go with the tried-and-true

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that this is what I asked in my other comment: it is because scala 2 runner supplied scala.boot.class.path. It would be worth revisiting what is supported under scala-cli. There is still -bootclasspath option. In scala 2, -nobootcp meant don't use -Xbootclasspath and don't supply scala.boot.classpath. Modern class loader hierarchy is quite different. I always use -nobootcp in scala 2.


/** Environment variables which java pays attention to so it
* seems we do as well.
*/
def classPathEnv: String = envOrElse("CLASSPATH", "")
def sourcePathEnv: String = envOrElse("SOURCEPATH", "")

def javaBootClassPath: String = propOrElse("sun.boot.class.path", searchForBootClasspath)
//using propOrNone/getOrElse instead of propOrElse so that searchForBootClasspath is lazy evaluated
def javaBootClassPath: String = propOrNone("sun.boot.class.path") getOrElse searchForBootClasspath
Comment on lines +56 to +57
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is a second commit, that is not part of the initial one that I was porting. But I think this improvement makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an improvement. propOrElse("user.dir", ???). This is the property that was removed at some point, I don't remember the details or naming. I see it is in JDK 8 but not now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, scala 2 at least supplied scala.boot.class.path as a workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an improvement

propOrElse is not lazy like in scala2, right? So this was an improvement...

Copy link
Contributor

@som-snytt som-snytt Dec 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a shame. Edit: I meant the usual lack of code sharing, not the improvement!


def javaExtDirs: String = propOrEmpty("java.ext.dirs")
def scalaHome: String = propOrEmpty("scala.home")
Expand Down
Loading