Skip to content

sorts: type parallel odd-even sort and propagate worker errors - #15493

Open
Ethereal49 wants to merge 2 commits into
TheAlgorithms:masterfrom
Ethereal49:fix/odd-even-parallel-comparable
Open

Ethereal49 wants to merge 2 commits into
TheAlgorithms:masterfrom
Ethereal49:fix/odd-even-parallel-comparable

Conversation

@Ethereal49

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

odd_even_transposition([1, "a"]) currently raises TypeError only in its workers, leaving the caller blocked waiting for a result. Add a local Comparable protocol and preserve the input element type, use only < comparisons, and send worker exceptions back to the caller. The parent waits for any result or worker exit, terminates and joins remaining workers on failure, closes all pipes, and writes back to the original list only after every result succeeds. Remove the private per-worker locks, which do not synchronize neighbors and otherwise leave semaphore warnings when blocked workers are terminated.

Part of #15234. This PR changes only sorts/odd_even_transposition_parallel.py and its dedicated cases in tests/test_sorts.py. The tracking issue explicitly requests code, doctests, and shared tests together; that specific scope is followed here despite the general template/AGENTS guidance to separate code and doctest changes. The existing ten-phase algorithm and empty-input behavior are outside this change.

Validation on macOS arm64, CPython 3.15.0rc2 free-threading build (GIL disabled):

  • Baseline at 32e2d504be1377957a4b6daf4cabe6a29c28489a: strings and int/float inputs sorted in place; [1, "a"] and [3, 2, "a", 1] each exceeded a three-second isolated-process timeout after worker comparison errors. Only the probe's own process group was killed.
  • Fixed isolated probes: both incompatible inputs return TypeError in under 0.1 seconds on this machine, preserve the input, leave no active worker children, and emit no resource warnings. Strings and mixed int/float inputs still sort in place.
  • uv sync --group=test: passed after an initial disk-space failure was resolved.
  • pytest tests/test_sorts.py sorts/odd_even_transposition_parallel.py --doctest-modules: 450 passed.
  • The same selection with CI flags --iterations=8 --parallel-threads=auto --ignore-gil-enabled and pytest-run-parallel: 450 passed.
  • Dedicated bounded pytest cases cover strings, mixed int/float, pickleable Person/Dog objects, incompatible inputs, list identity, unchanged input on failure, and no active worker children. POSIX process-group cleanup bounds these cases to ten seconds; those six cases explicitly skip on non-POSIX platforms.
  • Additional bounded probes passed for objects that support only __lt__, a worker that exits without a result, partial startup failure on an unpicklable item, and repeated failures followed by a successful sort.
  • uvx ruff check and uvx ruff format --check on both changed files, uvx --python 3.15 ty check on both changed files, and uvx pre-commit run --files sorts/odd_even_transposition_parallel.py tests/test_sorts.py: passed.

The full repository test suite, full-repository pre-commit, Ubuntu CI, and Windows execution were not run locally. No remote PR CI has run yet. Existing behavior for empty lists and more than ten phases is not addressed.

AI assistance: OpenAI Codex prepared this focused patch and executed the local validation above.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

The existing worker and demonstration functions do not have individual doctests; the public sorting function's doctests passed. This improves an existing algorithm and intentionally keeps the umbrella tracking issue open.

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Oct 5, 2026
@cclauss
cclauss requested a balanced review from Copilot October 5, 2026 06:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Singleton handling has a sentinel-index race, and unpickleable worker exceptions are masked.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Improves parallel odd-even transposition sort typing and worker failure handling.

Changes:

  • Adds generic comparable-item typing and <-only comparisons.
  • Propagates worker failures and cleans up processes/pipes.
  • Adds bounded multiprocessing tests for comparable and incompatible inputs.
File Description
sorts/​odd_even_transposition_parallel.py Adds typing, error propagation, and cleanup.
tests/​test_sorts.py Adds dedicated multiprocessing test cases.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +68 to +69
except Exception as error: # noqa: BLE001 -- propagate worker errors to the caller
result_pipe[1].send((None, error))
Comment on lines +190 to +193
sentinels = {
process.sentinel: position
for position, process in enumerate(process_array_)
}

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants