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

Fix BzlMod exception handling #18629

Open
katre opened this issue Jun 9, 2023 · 6 comments
Open

Fix BzlMod exception handling #18629

katre opened this issue Jun 9, 2023 · 6 comments
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests P3 We're not considering working on this, but happy to review a PR. (No assignee) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug

Comments

@katre
Copy link
Member

katre commented Jun 9, 2023

Almost all of the skyfunctions defined for bzlmod support have no exception handling, which means that when an exception does occur, Skyframe drops it and bazel crashes in an inconsistent state.

Issue #18567 is an example of this.

I've done a brief survey of the existing skyfunctions under src/main/java/com/google/devtools/build/lib/bazel/bzlmod/

  • BazelDepGraphFunction
    • key: BazelDepGraphValue.KEY -> singleton instance
    • calls:
      • BazelLockFileValue.KEY
        • no exceptions handled
      • BazelModuleResolutionValue.KEY
        • no exceptions handled
      • ModuleFileValue.key
        • no exceptions handled
      • ClientEnvironmentFunction.key
        • no exceptions handled
    • throws:
      • BazelDepGraphFunctionException
        • ExternalDepsException
  • BazelLockFileFunction
    • key: BazelLockFileValue.KEY -> singleton instance
    • calls:
      • FileValue.key
        • no exceptions handled
    • throws:
      • BazelLockfileFunctionException
        • ExternalDepsException
  • BazelModuleInspectorFunction
    • key: BazelModuleInspectorValue.KEY -> singleton instance
    • calls:
      • ModuleFileValue.KEY_FOR_ROOT_MODULE
      • BazelModuleResolutionValue.KEY
    • throws:
      • nothing
  • BazelModuleResolutionFunction
    • key: BazelModuleResolutionValue.KEY -> singleton instance
    • calls:
      • ClientEnvironmentFunction.key
        • no exceptions handled
      • ModuleFileValue.KEY_FOR_ROOT_MODULE
        • no exceptions handled
    • throws:
      • BazelModuleResolutionFunctionException
        • ExternalDepsException
  • ModuleFileFunction
    • key: ModuleFileValue.Key -> moduleKey, override
    • calls:
      • FileValue.key (twice)
        • no exceptions handled
      • RepositoryDirectoryValue.key
        • no exceptions handled
    • throws:
      • ModuleFileFunctionException
        • ExternalDepsException
  • SingleExtensionEvalFunction
    • key: ModuleExtensionId -> bzlFileLabel, extensionName
    • calls:
      • SingleExtensionUsagesValue.key
        • no exceptions handled
      • BzlLoadValue.keyForBzlmod
        • handles BzlLoadFailedException
    • throws:
      • SingleExtensionEvalFunctionException
        • ExternalDepsException
        • IOException
  • SingleExtensionUsagesFunction
    • key: ModuleExtensionId
    • calls:
      • BazelDepGraphValue.KEY
    • throws:
      • nothing

Every SkyKey can potentially throw the exceptions listed under its Skyfunction (for example, requesting BazelDepGraphValue.KEY can potentially raise ExternalDepsException.

Therefore, each of these functions needs to have existing env.getValue calls converted to env.getValueOrException calls, and then the exception needs to be handled properly, generally by re-throwing it.

@katre katre added type: bug P1 I'll work on this now. (Assignee required) area-Bzlmod Bzlmod-specific PRs, issues, and feature requests labels Jun 9, 2023
@katre katre self-assigned this Jun 9, 2023
@katre
Copy link
Member Author

katre commented Jun 9, 2023

@Wyverald Please tell me whether there are more skyfunctions that need to be audited.

@katre
Copy link
Member Author

katre commented Jun 9, 2023

  • BzlmodRepoRuleFunction
    • key: BzlmodRepoRuleValue.Key
    • calls:
    • ModuleFileValue.KEY_FOR_ROOT_MODULE
    • throws:
    • BzlmodRepoRuleFunctionException
      • InvalidRuleException
      • NoSuchPakcageException
      • EvalException
      • ExternalDepsException

@Wyverald
Copy link
Member

Wyverald commented Jun 9, 2023

Therefore, each of these functions needs to have existing env.getValue calls converted to env.getValueOrException calls, and then the exception needs to be handled properly, generally by re-throwing it.

I thought you'd only use env.getValueOrException when you can handle the exception in question? If you can't handle it, the normal procedure is to let it propagate naturally. (See javadoc for env.getValueOrThrow) We could also catch and re-throw but that should only happen if we can add more context (see javadoc for Skyfunction#compute)

What the Bzlmod skyfunctions are doing right now essentially means that they can't tolerate any exceptions. Hence whoever is requesting the Bzlmod skyvalues should be aware that they can throw exceptions of various types.

I'm not sure what's causing CTF to crash, but it seems to me that we need to fix CTF, not the Bzlmod skyfunctions. We could have the Bzlmod skyfunctions wrap the exception with more info, but that's an enhancement, not a bug fix.

@meisterT meisterT changed the title Fix BzlMode exception handling Fix BzlMod exception handling Jun 12, 2023
@katre
Copy link
Member Author

katre commented Jun 12, 2023

Okay, based on that I think we need to specifically make the following changes:

  1. Handle ExternalDepsException in PrerequisiteProducer
  2. Handle ExternalDepsException in ToolchaonResolutionFunction
    1. Toolchain external failures are different than direct dependencies and may need more context, like "why is this even a dependency?"
  3. Convert SingleExtensionEvalFunction's IOException to an ExternalDepsException.

We should also audit the other exceptions thrown from BzlmodRepoRuleFunction to ensure they are being handled somewhere.

copybara-service bot pushed a commit that referenced this issue Jun 12, 2023
This is a first step towards #18629.

Closes #18630.

PiperOrigin-RevId: 539698272
Change-Id: I1584c5fa47b591956609e05c8b55467de0f95e2e
katre added a commit to katre/bazel that referenced this issue Jun 12, 2023
copybara-service bot pushed a commit that referenced this issue Jun 12, 2023
ExternalDepsException.

Part of #18629.

Closes #18648.

PiperOrigin-RevId: 539754872
Change-Id: I3d31f823b89952eab2d5ce533947bceb3a1bc205
@katre
Copy link
Member Author

katre commented Jun 13, 2023

Okay, so I can deal with the error from #18567 if I add checks for ExternalDepsException at TargetPatternUtil.expandTargetPatterns.

However, if I add equivalent checks in SingleToolchainResolutionFunction, it's never called (because the BugReport.logUnexpected call in TargetPatternUtil has caused a crash already).

So, is the right answer to handle the exception in TargetPatternUtil? Is it to remove the BugReport.logUnexpected check? I can't figure out the right level for this.

traversaro pushed a commit to traversaro/bazel that referenced this issue Jun 24, 2023
This is a first step towards bazelbuild#18629.

Closes bazelbuild#18630.

PiperOrigin-RevId: 539698272
Change-Id: I1584c5fa47b591956609e05c8b55467de0f95e2e
traversaro pushed a commit to traversaro/bazel that referenced this issue Jun 24, 2023
ExternalDepsException.

Part of bazelbuild#18629.

Closes bazelbuild#18648.

PiperOrigin-RevId: 539754872
Change-Id: I3d31f823b89952eab2d5ce533947bceb3a1bc205
@katre katre removed their assignment Jun 26, 2023
@katre katre added untriaged and removed P1 I'll work on this now. (Assignee required) labels Jun 26, 2023
@katre
Copy link
Member Author

katre commented Jun 26, 2023

I can't work on this any further, so I am unassigning it and marking it as untriaged so that the bzlmod team can pick it up.

@meteorcloudy meteorcloudy added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Bzlmod Bzlmod-specific PRs, issues, and feature requests P3 We're not considering working on this, but happy to review a PR. (No assignee) team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. type: bug
Projects
None yet
Development

No branches or pull requests

3 participants