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

zsh: make zpty module work #2309

Merged
merged 1 commit into from
Jan 14, 2021
Merged

zsh: make zpty module work #2309

merged 1 commit into from
Jan 14, 2021

Conversation

SquallATF
Copy link
Contributor

@SquallATF SquallATF commented Jan 13, 2021

make zsh/zpty work with msys2, and can pass most mafredri/zsh-async#26 test.

    master = movefd(master);
    if (master == -1) {
	zerrnam(nam, "cannot duplicate fd %d: %e", master, errno);
	scriptname = oscriptname;
	ineval = oineval;
	return 1;
    }

Since cygwin uses two threads to process the pseudoterminal master device, zpty calls the movefd method to move the file description of the master device, and cygwin disposed the original fhandler_pty_master object, which causes the process thread to run incorrectly. here simply disable the zpty move the master device file descriptor untill cygwin upstream fix this issue.

  • zpty error strace
   30 27794711 [main] zsh 1918 fhandler_pty_master::cleanup: /dev/ptmx closing master, usecount 0
   43 27794754 [main] zsh 1918 fhandler_pty_master::close: closing from_master(0x310)/from_master_cyg(0x310)/to_master(0x264)/to_master_cyg(0x31C) since we own them(0)
   38 27794792 [main] zsh 1918 fhandler_pty_master::close: (2420): pty output_mutex (0x1F8): waiting -1 ms
   37 27794829 [main] zsh 1918 fhandler_pty_master::close: (2420): pty output_mutex: acquired
   71 27794900 [ptym] zsh 1918 fhandler_pty_master::pty_master_thread: ReadFile, Win32 error 6
   34 27794934 [ptym] zsh 1918 fhandler_pty_master::pty_master_thread: Reply: from 0x0, to 0x0, error 6
   37 27794971 [ptym] zsh 1918 fhandler_pty_master::pty_master_thread: WriteFile, Win32 error 6
   37 27795008 [ptym] zsh 1918 fhandler_pty_master::pty_master_thread: DisconnectNamedPipe, Win32 error 6
   39 27795047 [ptym] zsh 1918 fhandler_pty_master::pty_master_thread: Leaving
  • zsh-async test log
=== RUN   test__async_job_multiple_commands
--- PASS: test__async_job_multiple_commands (0.08s)
=== RUN   test__async_job_print_hi
--- PASS: test__async_job_print_hi (0.07s)
=== RUN   test__async_job_stderr
--- PASS: test__async_job_stderr (0.07s)
=== RUN   test__async_job_wait_for_token
--- PASS: test__async_job_wait_for_token (0.19s)
=== RUN   test_all_options
--- FAIL: test_all_options (15.05s)
        ./async_test.zsh:517: timed out
=== RUN   test_async_flush_jobs
--- PASS: test_async_flush_jobs (0.63s)
=== RUN   test_async_job_error_and_nonzero_exit
--- PASS: test_async_job_error_and_nonzero_exit (0.24s)
=== RUN   test_async_job_git_status
--- PASS: test_async_job_git_status (0.43s)
=== RUN   test_async_job_keeps_nulls
--- PASS: test_async_job_keeps_nulls (0.21s)
=== RUN   test_async_job_multiple_arguments_and_spaces
--- PASS: test_async_job_multiple_arguments_and_spaces (0.27s)
=== RUN   test_async_job_multiple_commands_in_multiline_string
--- PASS: test_async_job_multiple_commands_in_multiline_string (0.22s)
=== RUN   test_async_job_print_matches_input_exactly
--- PASS: test_async_job_print_matches_input_exactly (0.21s)
=== RUN   test_async_job_unique_worker
--- PASS: test_async_job_unique_worker (0.49s)
=== RUN   test_async_job_with_rc_expand_param
--- PASS: test_async_job_with_rc_expand_param (0.19s)
=== RUN   test_async_process_results
--- PASS: test_async_process_results (0.25s)
=== RUN   test_async_process_results_stress
--- FAIL: test_async_process_results_stress (9.13s)
        ./async_test.zsh:182: timed out after 5s
        ./async_test.zsh:183: wanted 20 results, got 14
=== RUN   test_async_start_stop_worker
--- PASS: test_async_start_stop_worker (0.06s)
=== RUN   test_async_worker_notify_sigwinch
--- PASS: test_async_worker_notify_sigwinch (2.44s)
=== RUN   test_async_worker_survives_termination_of_other_worker
--- PASS: test_async_worker_survives_termination_of_other_worker (0.15s)
=== RUN   test_async_worker_update_pwd
--- PASS: test_async_worker_update_pwd (0.33s)
=== RUN   test_async_worker_update_pwd_and_env
--- PASS: test_async_worker_update_pwd_and_env (0.36s)
=== RUN   test_zle_watcher
--- SKIP: test_zle_watcher (0.01s)
        ./async_test.zsh:596: Test is not reliable on zsh 5.0.X
FAIL
exit code 1
FAIL    ./async_test.zsh        31.139s

@SquallATF SquallATF changed the title make zpty module work zsh: make zpty module work Jan 13, 2021
zsh/fix-zpty-module.patch Outdated Show resolved Hide resolved
@lazka
Copy link
Member

lazka commented Jan 13, 2021

Is there any upstream issue for this (zsh or cygwin)?

@SquallATF SquallATF force-pushed the zsh/zpty branch 2 times, most recently from 54b83e6 to af72610 Compare January 14, 2021 01:10
@SquallATF
Copy link
Contributor Author

SquallATF commented Jan 14, 2021

There is a cygwin upstream issue but patched zpty to avoid this issue.
zpty invoke movefd will dup and close original fd of /dev/ptmx and cause the fhandler_pty_master object to be recycled, which will damage pty_master_thread and pty_master_fwd_thread threads on that object.

Here is a poc to reproduce cygwin issue, program will hang when close(mfd) at last .

#define _GNU_SOURCE 1
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {
  static char *name;
  static int mfd, sfd;
  if ((mfd = posix_openpt(O_RDWR|O_NOCTTY)) < 0) {
    printf("posix_openpt failed %d\n", mfd);
    return 1;
  }

  if (grantpt(mfd) || unlockpt(mfd) || !(name = ptsname(mfd))) {
    printf("some failed\n");
    close(mfd);
    return 1;
  }
  int fd;
  if((fd = dup(mfd)) < 0) {
    printf("dup failed %d\n", fd);
    return 1;
  }
  close(mfd);
  mfd = fd;
  if ((sfd = open(name, O_RDWR)) < 0) {
    printf("open %s failed\n", name);
    return 1;
  }

  printf("before close mfd %d\n", mfd);
  close(mfd);
  printf("after close mfd %d\n", mfd);
  close(sfd);  
  printf("before close sfd %d\n", sfd);

  return 0;
}

zsh/PKGBUILD Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants