MCPcopy
hub / github.com/containers/toolbox / discardInputAsync

Function discardInputAsync

src/cmd/utils.go:176–261  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

174}
175
176func discardInputAsync(ctx context.Context) (<-chan int, <-chan error) {
177 retValCh := make(chan int, 1)
178 errCh := make(chan error, 1)
179
180 done := ctx.Done()
181 eventFD := -1
182 if done != nil {
183 fd, err := unix.Eventfd(0, unix.EFD_CLOEXEC|unix.EFD_NONBLOCK)
184 if err != nil {
185 errCh <- fmt.Errorf("eventfd(2) failed: %w", err)
186 return retValCh, errCh
187 }
188
189 eventFD = fd
190 }
191
192 go func() {
193 var total int
194
195 for {
196 pollFn := func(errPoll error, pollFDs []unix.PollFd) error {
197 if len(pollFDs) != 1 {
198 panic("unexpected number of file descriptors")
199 }
200
201 if errPoll != nil && !errors.Is(errPoll, context.Canceled) {
202 return errPoll
203 }
204
205 if pollFDs[0].Revents&unix.POLLIN != 0 {
206 logrus.Debug("Returned from /dev/stdin: POLLIN")
207
208 buffer := make([]byte, bytes.MinRead)
209 n, err := os.Stdin.Read(buffer)
210 total += n
211
212 if errPoll != nil {
213 return errPoll
214 } else if err != nil {
215 return err
216 }
217
218 return nil
219 }
220
221 if pollFDs[0].Revents&unix.POLLHUP != 0 {
222 logrus.Debug("Returned from /dev/stdin: POLLHUP")
223
224 if errPoll != nil {
225 return errPoll
226 }
227
228 return errHUP
229 }
230
231 if pollFDs[0].Revents&unix.POLLNVAL != 0 {
232 logrus.Debug("Returned from /dev/stdin: POLLNVAL")
233

Callers 1

Calls 3

pollFunction · 0.85
watchContextForEventFDFunction · 0.85
IsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…